2
0

text_edit.cpp 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686
  1. /*************************************************************************/
  2. /* text_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "message_queue.h"
  32. #include "os/input.h"
  33. #include "os/keyboard.h"
  34. #include "os/os.h"
  35. #include "project_settings.h"
  36. #include "scene/main/viewport.h"
  37. #ifdef TOOLS_ENABLED
  38. #include "editor/editor_scale.h"
  39. #endif
  40. #define TAB_PIXELS
  41. static bool _is_text_char(CharType c) {
  42. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  43. }
  44. static bool _is_symbol(CharType c) {
  45. return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' ');
  46. }
  47. static bool _is_whitespace(CharType c) {
  48. return c == '\t' || c == ' ';
  49. }
  50. static bool _is_char(CharType c) {
  51. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
  52. }
  53. static bool _is_number(CharType c) {
  54. return (c >= '0' && c <= '9');
  55. }
  56. static bool _is_hex_symbol(CharType c) {
  57. return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  58. }
  59. static bool _is_pair_right_symbol(CharType c) {
  60. return c == '"' ||
  61. c == '\'' ||
  62. c == ')' ||
  63. c == ']' ||
  64. c == '}';
  65. }
  66. static bool _is_pair_left_symbol(CharType c) {
  67. return c == '"' ||
  68. c == '\'' ||
  69. c == '(' ||
  70. c == '[' ||
  71. c == '{';
  72. }
  73. static bool _is_pair_symbol(CharType c) {
  74. return _is_pair_left_symbol(c) || _is_pair_right_symbol(c);
  75. }
  76. static CharType _get_right_pair_symbol(CharType c) {
  77. if (c == '"')
  78. return '"';
  79. if (c == '\'')
  80. return '\'';
  81. if (c == '(')
  82. return ')';
  83. if (c == '[')
  84. return ']';
  85. if (c == '{')
  86. return '}';
  87. return 0;
  88. }
  89. void TextEdit::Text::set_font(const Ref<Font> &p_font) {
  90. font = p_font;
  91. }
  92. void TextEdit::Text::set_indent_size(int p_indent_size) {
  93. indent_size = p_indent_size;
  94. }
  95. void TextEdit::Text::_update_line_cache(int p_line) const {
  96. int w = 0;
  97. int tab_w = font->get_char_size(' ').width * indent_size;
  98. int len = text[p_line].data.length();
  99. const CharType *str = text[p_line].data.c_str();
  100. //update width
  101. for (int i = 0; i < len; i++) {
  102. if (str[i] == '\t') {
  103. int left = w % tab_w;
  104. if (left == 0)
  105. w += tab_w;
  106. else
  107. w += tab_w - w % tab_w; // is right...
  108. } else {
  109. w += font->get_char_size(str[i], str[i + 1]).width;
  110. }
  111. }
  112. text[p_line].width_cache = w;
  113. //update regions
  114. text[p_line].region_info.clear();
  115. for (int i = 0; i < len; i++) {
  116. if (!_is_symbol(str[i]))
  117. continue;
  118. if (str[i] == '\\') {
  119. i++; //skip quoted anything
  120. continue;
  121. }
  122. int left = len - i;
  123. for (int j = 0; j < color_regions->size(); j++) {
  124. const ColorRegion &cr = color_regions->operator[](j);
  125. /* BEGIN */
  126. int lr = cr.begin_key.length();
  127. if (lr == 0 || lr > left)
  128. continue;
  129. const CharType *kc = cr.begin_key.c_str();
  130. bool match = true;
  131. for (int k = 0; k < lr; k++) {
  132. if (kc[k] != str[i + k]) {
  133. match = false;
  134. break;
  135. }
  136. }
  137. if (match) {
  138. ColorRegionInfo cri;
  139. cri.end = false;
  140. cri.region = j;
  141. text[p_line].region_info[i] = cri;
  142. i += lr - 1;
  143. break;
  144. }
  145. /* END */
  146. lr = cr.end_key.length();
  147. if (lr == 0 || lr > left)
  148. continue;
  149. kc = cr.end_key.c_str();
  150. match = true;
  151. for (int k = 0; k < lr; k++) {
  152. if (kc[k] != str[i + k]) {
  153. match = false;
  154. break;
  155. }
  156. }
  157. if (match) {
  158. ColorRegionInfo cri;
  159. cri.end = true;
  160. cri.region = j;
  161. text[p_line].region_info[i] = cri;
  162. i += lr - 1;
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. const Map<int, TextEdit::Text::ColorRegionInfo> &TextEdit::Text::get_color_region_info(int p_line) {
  169. static Map<int, ColorRegionInfo> cri;
  170. ERR_FAIL_INDEX_V(p_line, text.size(), cri);
  171. if (text[p_line].width_cache == -1) {
  172. _update_line_cache(p_line);
  173. }
  174. return text[p_line].region_info;
  175. }
  176. int TextEdit::Text::get_line_width(int p_line) const {
  177. ERR_FAIL_INDEX_V(p_line, text.size(), -1);
  178. if (text[p_line].width_cache == -1) {
  179. _update_line_cache(p_line);
  180. }
  181. return text[p_line].width_cache;
  182. }
  183. void TextEdit::Text::clear_caches() {
  184. for (int i = 0; i < text.size(); i++)
  185. text[i].width_cache = -1;
  186. }
  187. void TextEdit::Text::clear() {
  188. text.clear();
  189. insert(0, "");
  190. }
  191. int TextEdit::Text::get_max_width(bool p_exclude_hidden) const {
  192. //quite some work.. but should be fast enough.
  193. int max = 0;
  194. for (int i = 0; i < text.size(); i++) {
  195. if (!p_exclude_hidden || !is_hidden(i))
  196. max = MAX(max, get_line_width(i));
  197. }
  198. return max;
  199. }
  200. void TextEdit::Text::set(int p_line, const String &p_text) {
  201. ERR_FAIL_INDEX(p_line, text.size());
  202. text[p_line].width_cache = -1;
  203. text[p_line].data = p_text;
  204. }
  205. void TextEdit::Text::insert(int p_at, const String &p_text) {
  206. Line line;
  207. line.marked = false;
  208. line.breakpoint = false;
  209. line.hidden = false;
  210. line.width_cache = -1;
  211. line.data = p_text;
  212. text.insert(p_at, line);
  213. }
  214. void TextEdit::Text::remove(int p_at) {
  215. text.remove(p_at);
  216. }
  217. void TextEdit::_update_scrollbars() {
  218. Size2 size = get_size();
  219. Size2 hmin = h_scroll->get_combined_minimum_size();
  220. Size2 vmin = v_scroll->get_combined_minimum_size();
  221. v_scroll->set_begin(Point2(size.width - vmin.width, cache.style_normal->get_margin(MARGIN_TOP)));
  222. v_scroll->set_end(Point2(size.width, size.height - cache.style_normal->get_margin(MARGIN_TOP) - cache.style_normal->get_margin(MARGIN_BOTTOM)));
  223. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  224. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  225. int hscroll_rows = ((hmin.height - 1) / get_row_height()) + 1;
  226. int visible_rows = get_visible_rows();
  227. int num_rows = MAX(visible_rows, num_lines_from(CLAMP(cursor.line_ofs, 0, text.size() - 1), MIN(visible_rows, text.size() - 1 - cursor.line_ofs)));
  228. int total_rows = (is_hiding_enabled() ? get_total_unhidden_rows() : text.size());
  229. if (scroll_past_end_of_file_enabled) {
  230. total_rows += visible_rows - 1;
  231. }
  232. int vscroll_pixels = v_scroll->get_combined_minimum_size().width;
  233. int visible_width = size.width - cache.style_normal->get_minimum_size().width;
  234. int total_width = text.get_max_width(true) + vmin.x;
  235. if (line_numbers)
  236. total_width += cache.line_number_w;
  237. if (draw_breakpoint_gutter) {
  238. total_width += cache.breakpoint_gutter_width;
  239. }
  240. if (draw_fold_gutter) {
  241. total_width += cache.fold_gutter_width;
  242. }
  243. bool use_hscroll = true;
  244. bool use_vscroll = true;
  245. if (total_rows <= visible_rows && total_width <= visible_width) {
  246. //thanks yessopie for this clever bit of logic
  247. use_hscroll = false;
  248. use_vscroll = false;
  249. } else {
  250. if (total_rows > visible_rows && total_width <= visible_width - vscroll_pixels) {
  251. //thanks yessopie for this clever bit of logic
  252. use_hscroll = false;
  253. }
  254. if (total_rows <= visible_rows - hscroll_rows && total_width > visible_width) {
  255. //thanks yessopie for this clever bit of logic
  256. use_vscroll = false;
  257. }
  258. }
  259. updating_scrolls = true;
  260. if (use_vscroll) {
  261. v_scroll->show();
  262. v_scroll->set_max(total_rows);
  263. v_scroll->set_page(visible_rows);
  264. if (smooth_scroll_enabled) {
  265. v_scroll->set_step(0.25);
  266. } else {
  267. v_scroll->set_step(1);
  268. }
  269. update_line_scroll_pos();
  270. if (fabs(v_scroll->get_value() - get_line_scroll_pos()) >= 1) {
  271. cursor.line_ofs += v_scroll->get_value() - get_line_scroll_pos();
  272. }
  273. } else {
  274. cursor.line_ofs = 0;
  275. line_scroll_pos = 0;
  276. v_scroll->set_value(0);
  277. v_scroll->hide();
  278. }
  279. if (use_hscroll) {
  280. h_scroll->show();
  281. h_scroll->set_max(total_width);
  282. h_scroll->set_page(visible_width);
  283. if (cursor.x_ofs > (total_width - visible_width))
  284. cursor.x_ofs = (total_width - visible_width);
  285. if (fabs(h_scroll->get_value() - (double)cursor.x_ofs) >= 1) {
  286. h_scroll->set_value(cursor.x_ofs);
  287. }
  288. } else {
  289. cursor.x_ofs = 0;
  290. h_scroll->set_value(0);
  291. h_scroll->hide();
  292. }
  293. updating_scrolls = false;
  294. }
  295. void TextEdit::_click_selection_held() {
  296. if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && selection.selecting_mode != Selection::MODE_NONE) {
  297. switch (selection.selecting_mode) {
  298. case Selection::MODE_POINTER: {
  299. _update_selection_mode_pointer();
  300. } break;
  301. case Selection::MODE_WORD: {
  302. _update_selection_mode_word();
  303. } break;
  304. case Selection::MODE_LINE: {
  305. _update_selection_mode_line();
  306. } break;
  307. default: {
  308. break;
  309. }
  310. }
  311. } else {
  312. click_select_held->stop();
  313. }
  314. }
  315. void TextEdit::_update_selection_mode_pointer() {
  316. Point2 mp = Input::get_singleton()->get_mouse_position() - get_global_position();
  317. int row, col;
  318. _get_mouse_pos(Point2i(mp.x, mp.y), row, col);
  319. select(selection.selecting_line, selection.selecting_column, row, col);
  320. cursor_set_line(row);
  321. cursor_set_column(col);
  322. update();
  323. click_select_held->start();
  324. }
  325. void TextEdit::_update_selection_mode_word() {
  326. Point2 mp = Input::get_singleton()->get_mouse_position() - get_global_position();
  327. int row, col;
  328. _get_mouse_pos(Point2i(mp.x, mp.y), row, col);
  329. String line = text[row];
  330. int beg = CLAMP(col, 0, line.length());
  331. // if its the first selection and on whitespace make sure we grab the word instead..
  332. if (!selection.active) {
  333. while (beg > 0 && line[beg] <= 32) {
  334. beg--;
  335. }
  336. }
  337. int end = beg;
  338. bool symbol = beg < line.length() && _is_symbol(line[beg]);
  339. // get the word end and begin points
  340. while (beg > 0 && line[beg - 1] > 32 && (symbol == _is_symbol(line[beg - 1]))) {
  341. beg--;
  342. }
  343. while (end < line.length() && line[end + 1] > 32 && (symbol == _is_symbol(line[end + 1]))) {
  344. end++;
  345. }
  346. if (end < line.length()) {
  347. end += 1;
  348. }
  349. // inital selection
  350. if (!selection.active) {
  351. select(row, beg, row, end);
  352. selection.selecting_column = beg;
  353. selection.selected_word_beg = beg;
  354. selection.selected_word_end = end;
  355. selection.selected_word_origin = beg;
  356. cursor_set_column(selection.to_column);
  357. } else {
  358. if ((col <= selection.selected_word_origin && row == selection.selecting_line) || row < selection.selecting_line) {
  359. selection.selecting_column = selection.selected_word_end;
  360. select(row, beg, selection.selecting_line, selection.selected_word_end);
  361. cursor_set_column(selection.from_column);
  362. } else {
  363. selection.selecting_column = selection.selected_word_beg;
  364. select(selection.selecting_line, selection.selected_word_beg, row, end);
  365. cursor_set_column(selection.to_column);
  366. }
  367. }
  368. cursor_set_line(row);
  369. update();
  370. click_select_held->start();
  371. }
  372. void TextEdit::_update_selection_mode_line() {
  373. Point2 mp = Input::get_singleton()->get_mouse_position() - get_global_position();
  374. int row, col;
  375. _get_mouse_pos(Point2i(mp.x, mp.y), row, col);
  376. col = 0;
  377. if (row < selection.selecting_line) {
  378. // cursor is above us
  379. cursor_set_line(row - 1);
  380. selection.selecting_column = text[selection.selecting_line].length();
  381. } else {
  382. // cursor is below us
  383. cursor_set_line(row + 1);
  384. selection.selecting_column = 0;
  385. col = text[row].length();
  386. }
  387. cursor_set_column(0);
  388. select(selection.selecting_line, selection.selecting_column, row, col);
  389. update();
  390. click_select_held->start();
  391. }
  392. void TextEdit::_notification(int p_what) {
  393. switch (p_what) {
  394. case NOTIFICATION_ENTER_TREE: {
  395. _update_caches();
  396. if (cursor_changed_dirty)
  397. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  398. if (text_changed_dirty)
  399. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  400. } break;
  401. case NOTIFICATION_RESIZED: {
  402. cache.size = get_size();
  403. adjust_viewport_to_cursor();
  404. } break;
  405. case NOTIFICATION_THEME_CHANGED: {
  406. _update_caches();
  407. } break;
  408. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  409. window_has_focus = true;
  410. draw_caret = true;
  411. update();
  412. } break;
  413. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  414. window_has_focus = false;
  415. draw_caret = false;
  416. update();
  417. } break;
  418. case NOTIFICATION_PHYSICS_PROCESS: {
  419. if (scrolling && v_scroll->get_value() != target_v_scroll) {
  420. double target_y = target_v_scroll - v_scroll->get_value();
  421. double dist = sqrt(target_y * target_y);
  422. double vel = ((target_y / dist) * v_scroll_speed) * get_physics_process_delta_time();
  423. if (Math::abs(vel) >= dist) {
  424. v_scroll->set_value(target_v_scroll);
  425. scrolling = false;
  426. set_physics_process(false);
  427. } else {
  428. v_scroll->set_value(v_scroll->get_value() + vel);
  429. }
  430. } else {
  431. scrolling = false;
  432. set_physics_process(false);
  433. }
  434. } break;
  435. case NOTIFICATION_DRAW: {
  436. if ((!has_focus() && !menu->has_focus()) || !window_has_focus) {
  437. draw_caret = false;
  438. }
  439. if (draw_breakpoint_gutter) {
  440. breakpoint_gutter_width = (get_row_height() * 55) / 100;
  441. cache.breakpoint_gutter_width = breakpoint_gutter_width;
  442. } else {
  443. cache.breakpoint_gutter_width = 0;
  444. }
  445. if (draw_fold_gutter) {
  446. fold_gutter_width = (get_row_height() * 55) / 100;
  447. cache.fold_gutter_width = fold_gutter_width;
  448. } else {
  449. cache.fold_gutter_width = 0;
  450. }
  451. int line_number_char_count = 0;
  452. {
  453. int lc = text.size();
  454. cache.line_number_w = 0;
  455. while (lc) {
  456. cache.line_number_w += 1;
  457. lc /= 10;
  458. };
  459. if (line_numbers) {
  460. line_number_char_count = cache.line_number_w;
  461. cache.line_number_w = (cache.line_number_w + 1) * cache.font->get_char_size('0').width;
  462. } else {
  463. cache.line_number_w = 0;
  464. }
  465. }
  466. _update_scrollbars();
  467. RID ci = get_canvas_item();
  468. VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  469. int xmargin_beg = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width;
  470. int xmargin_end = cache.size.width - cache.style_normal->get_margin(MARGIN_RIGHT);
  471. //let's do it easy for now:
  472. cache.style_normal->draw(ci, Rect2(Point2(), cache.size));
  473. float readonly_alpha = 1.0; // used to set the input text color when in read-only mode
  474. if (readonly) {
  475. cache.style_readonly->draw(ci, Rect2(Point2(), cache.size));
  476. readonly_alpha = .5;
  477. draw_caret = false;
  478. }
  479. if (has_focus())
  480. cache.style_focus->draw(ci, Rect2(Point2(), cache.size));
  481. int ascent = cache.font->get_ascent();
  482. int visible_rows = get_visible_rows() + 1;
  483. int tab_w = cache.font->get_char_size(' ').width * indent_size;
  484. Color color = cache.font_color;
  485. color.a *= readonly_alpha;
  486. int in_region = -1;
  487. if (syntax_coloring) {
  488. if (cache.background_color.a > 0.01) {
  489. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), cache.background_color);
  490. }
  491. //compute actual region to start (may be inside say, a comment).
  492. //slow in very large documments :( but ok for source!
  493. for (int i = 0; i < cursor.line_ofs; i++) {
  494. const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(i);
  495. if (in_region >= 0 && color_regions[in_region].line_only) {
  496. in_region = -1; //reset regions that end at end of line
  497. }
  498. for (const Map<int, Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) {
  499. const Text::ColorRegionInfo &cri = E->get();
  500. if (in_region == -1) {
  501. if (!cri.end) {
  502. in_region = cri.region;
  503. }
  504. } else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
  505. if (cri.end || color_regions[cri.region].eq) {
  506. in_region = -1;
  507. }
  508. }
  509. }
  510. }
  511. }
  512. int brace_open_match_line = -1;
  513. int brace_open_match_column = -1;
  514. bool brace_open_matching = false;
  515. bool brace_open_mismatch = false;
  516. int brace_close_match_line = -1;
  517. int brace_close_match_column = -1;
  518. bool brace_close_matching = false;
  519. bool brace_close_mismatch = false;
  520. if (brace_matching_enabled) {
  521. if (cursor.column < text[cursor.line].length()) {
  522. //check for open
  523. CharType c = text[cursor.line][cursor.column];
  524. CharType closec = 0;
  525. if (c == '[') {
  526. closec = ']';
  527. } else if (c == '{') {
  528. closec = '}';
  529. } else if (c == '(') {
  530. closec = ')';
  531. }
  532. if (closec != 0) {
  533. int stack = 1;
  534. for (int i = cursor.line; i < text.size(); i++) {
  535. int from = i == cursor.line ? cursor.column + 1 : 0;
  536. for (int j = from; j < text[i].length(); j++) {
  537. CharType cc = text[i][j];
  538. //ignore any brackets inside a string
  539. if (cc == '"' || cc == '\'') {
  540. CharType quotation = cc;
  541. do {
  542. j++;
  543. if (!(j < text[i].length())) {
  544. break;
  545. }
  546. cc = text[i][j];
  547. //skip over escaped quotation marks inside strings
  548. if (cc == '\\') {
  549. bool escaped = true;
  550. while (j + 1 < text[i].length() && text[i][j + 1] == '\\') {
  551. escaped = !escaped;
  552. j++;
  553. }
  554. if (escaped) {
  555. j++;
  556. continue;
  557. }
  558. }
  559. } while (cc != quotation);
  560. } else if (cc == c)
  561. stack++;
  562. else if (cc == closec)
  563. stack--;
  564. if (stack == 0) {
  565. brace_open_match_line = i;
  566. brace_open_match_column = j;
  567. brace_open_matching = true;
  568. break;
  569. }
  570. }
  571. if (brace_open_match_line != -1)
  572. break;
  573. }
  574. if (!brace_open_matching)
  575. brace_open_mismatch = true;
  576. }
  577. }
  578. if (cursor.column > 0) {
  579. CharType c = text[cursor.line][cursor.column - 1];
  580. CharType closec = 0;
  581. if (c == ']') {
  582. closec = '[';
  583. } else if (c == '}') {
  584. closec = '{';
  585. } else if (c == ')') {
  586. closec = '(';
  587. }
  588. if (closec != 0) {
  589. int stack = 1;
  590. for (int i = cursor.line; i >= 0; i--) {
  591. int from = i == cursor.line ? cursor.column - 2 : text[i].length() - 1;
  592. for (int j = from; j >= 0; j--) {
  593. CharType cc = text[i][j];
  594. //ignore any brackets inside a string
  595. if (cc == '"' || cc == '\'') {
  596. CharType quotation = cc;
  597. do {
  598. j--;
  599. if (!(j >= 0)) {
  600. break;
  601. }
  602. cc = text[i][j];
  603. //skip over escaped quotation marks inside strings
  604. if (cc == quotation) {
  605. bool escaped = false;
  606. while (j - 1 >= 0 && text[i][j - 1] == '\\') {
  607. escaped = !escaped;
  608. j--;
  609. }
  610. if (escaped) {
  611. j--;
  612. cc = '\\';
  613. continue;
  614. }
  615. }
  616. } while (cc != quotation);
  617. } else if (cc == c)
  618. stack++;
  619. else if (cc == closec)
  620. stack--;
  621. if (stack == 0) {
  622. brace_close_match_line = i;
  623. brace_close_match_column = j;
  624. brace_close_matching = true;
  625. break;
  626. }
  627. }
  628. if (brace_close_match_line != -1)
  629. break;
  630. }
  631. if (!brace_close_matching)
  632. brace_close_mismatch = true;
  633. }
  634. }
  635. }
  636. int deregion = 0; //force it to clear inrgion
  637. Point2 cursor_pos;
  638. // get the highlighted words
  639. String highlighted_text = get_selection_text();
  640. String line_num_padding = line_numbers_zero_padded ? "0" : " ";
  641. update_line_scroll_pos();
  642. int line = cursor.line_ofs - 1;
  643. // another row may be visible during smooth scrolling
  644. int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0);
  645. for (int i = 0; i < draw_amount; i++) {
  646. line++;
  647. if (line < 0 || line >= (int)text.size())
  648. continue;
  649. while (is_line_hidden(line)) {
  650. line++;
  651. if (line < 0 || line >= (int)text.size()) {
  652. break;
  653. }
  654. }
  655. if (line < 0 || line >= (int)text.size())
  656. continue;
  657. const String &str = text[line];
  658. int char_margin = xmargin_beg - cursor.x_ofs;
  659. int char_ofs = 0;
  660. int ofs_readonly = 0;
  661. int ofs_x = 0;
  662. if (readonly) {
  663. ofs_readonly = cache.style_readonly->get_offset().y / 2;
  664. ofs_x = cache.style_readonly->get_offset().x / 2;
  665. }
  666. int ofs_y = (i * get_row_height() + cache.line_spacing / 2) + ofs_readonly;
  667. if (smooth_scroll_enabled)
  668. ofs_y -= ((v_scroll->get_value() - get_line_scroll_pos()) * get_row_height());
  669. bool prev_is_char = false;
  670. bool prev_is_number = false;
  671. bool in_keyword = false;
  672. bool underlined = false;
  673. bool in_word = false;
  674. bool in_function_name = false;
  675. bool in_member_variable = false;
  676. bool is_hex_notation = false;
  677. Color keyword_color;
  678. // check if line contains highlighted word
  679. int highlighted_text_col = -1;
  680. int search_text_col = -1;
  681. if (!search_text.empty())
  682. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
  683. if (highlighted_text.length() != 0 && highlighted_text != search_text)
  684. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  685. const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(line);
  686. if (text.is_marked(line)) {
  687. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.mark_color);
  688. }
  689. if (str.length() == 0) {
  690. // draw line background if empty as we won't loop at at all
  691. if (line == cursor.line && highlight_current_line) {
  692. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, get_row_height()), cache.current_line_color);
  693. }
  694. // give visual indication of empty selected line
  695. if (selection.active && line >= selection.from_line && line <= selection.to_line) {
  696. int char_w = cache.font->get_char_size(' ').width;
  697. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, char_w, get_row_height()), cache.selection_color);
  698. }
  699. } else {
  700. // if it has text, then draw current line marker in the margin, as line number ect will draw over it, draw the rest of line marker later.
  701. if (line == cursor.line && highlight_current_line) {
  702. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(0, ofs_y, xmargin_beg, get_row_height()), cache.current_line_color);
  703. }
  704. }
  705. if (text.is_breakpoint(line) && !draw_breakpoint_gutter) {
  706. #ifdef TOOLS_ENABLED
  707. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y + get_row_height() - EDSCALE, xmargin_end - xmargin_beg, EDSCALE), cache.breakpoint_color);
  708. #else
  709. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.breakpoint_color);
  710. #endif
  711. }
  712. // draw breakpoint marker
  713. if (text.is_breakpoint(line)) {
  714. if (draw_breakpoint_gutter) {
  715. int vertical_gap = (get_row_height() * 40) / 100;
  716. int horizontal_gap = (cache.breakpoint_gutter_width * 30) / 100;
  717. int marker_height = get_row_height() - (vertical_gap * 2);
  718. int marker_width = cache.breakpoint_gutter_width - (horizontal_gap * 2);
  719. // no transparency on marker
  720. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cache.style_normal->get_margin(MARGIN_LEFT) + horizontal_gap - 2, ofs_y + vertical_gap, marker_width, marker_height), Color(cache.breakpoint_color.r, cache.breakpoint_color.g, cache.breakpoint_color.b));
  721. }
  722. }
  723. // draw fold markers
  724. if (draw_fold_gutter) {
  725. int horizontal_gap = (cache.fold_gutter_width * 30) / 100;
  726. int gutter_left = cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width + cache.line_number_w;
  727. if (is_folded(line)) {
  728. int xofs = horizontal_gap - (cache.can_fold_icon->get_width()) / 2;
  729. int yofs = (get_row_height() - cache.folded_icon->get_height()) / 2;
  730. cache.folded_icon->draw(ci, Point2(gutter_left + xofs + ofs_x, ofs_y + yofs), cache.code_folding_color);
  731. } else if (can_fold(line)) {
  732. int xofs = -cache.can_fold_icon->get_width() / 2 - horizontal_gap + 3;
  733. int yofs = (get_row_height() - cache.can_fold_icon->get_height()) / 2;
  734. cache.can_fold_icon->draw(ci, Point2(gutter_left + xofs + ofs_x, ofs_y + yofs), cache.code_folding_color);
  735. }
  736. }
  737. if (cache.line_number_w) {
  738. String fc = String::num(line + 1);
  739. while (fc.length() < line_number_char_count) {
  740. fc = line_num_padding + fc;
  741. }
  742. cache.font->draw(ci, Point2(cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width + ofs_x, ofs_y + cache.font->get_ascent()), fc, cache.line_number_color);
  743. }
  744. //loop through charcters in one line
  745. for (int j = 0; j < str.length(); j++) {
  746. //look for keyword
  747. if (deregion > 0) {
  748. deregion--;
  749. if (deregion == 0)
  750. in_region = -1;
  751. }
  752. if (syntax_coloring && deregion == 0) {
  753. color = cache.font_color; //reset
  754. color.a *= readonly_alpha;
  755. //find keyword
  756. bool is_char = _is_text_char(str[j]);
  757. bool is_symbol = _is_symbol(str[j]);
  758. bool is_number = _is_number(str[j]);
  759. if (j == 0 && in_region >= 0 && color_regions[in_region].line_only) {
  760. in_region = -1; //reset regions that end at end of line
  761. }
  762. // allow ABCDEF in hex notation
  763. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  764. is_number = true;
  765. } else {
  766. is_hex_notation = false;
  767. }
  768. // check for dot or underscore or 'x' for hex notation in floating point number
  769. if ((str[j] == '.' || str[j] == 'x' || str[j] == '_') && !in_word && prev_is_number && !is_number) {
  770. is_number = true;
  771. is_symbol = false;
  772. is_char = false;
  773. if (str[j] == 'x' && str[j - 1] == '0') {
  774. is_hex_notation = true;
  775. }
  776. }
  777. if (!in_word && _is_char(str[j]) && !is_number) {
  778. in_word = true;
  779. }
  780. if ((in_keyword || in_word) && !is_hex_notation) {
  781. is_number = false;
  782. }
  783. if (is_symbol && str[j] != '.' && in_word) {
  784. in_word = false;
  785. }
  786. if (is_symbol && cri_map.has(j)) {
  787. const Text::ColorRegionInfo &cri = cri_map[j];
  788. if (in_region == -1) {
  789. if (!cri.end) {
  790. in_region = cri.region;
  791. }
  792. } else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
  793. if (cri.end || color_regions[cri.region].eq) {
  794. deregion = color_regions[cri.region].eq ? color_regions[cri.region].begin_key.length() : color_regions[cri.region].end_key.length();
  795. }
  796. }
  797. }
  798. if (!is_char) {
  799. in_keyword = false;
  800. underlined = false;
  801. }
  802. if (in_region == -1 && !in_keyword && is_char && !prev_is_char) {
  803. int to = j;
  804. while (to < str.length() && _is_text_char(str[to]))
  805. to++;
  806. uint32_t hash = String::hash(&str[j], to - j);
  807. StrRange range(&str[j], to - j);
  808. const Color *col = keywords.custom_getptr(range, hash);
  809. if (col) {
  810. in_keyword = true;
  811. keyword_color = *col;
  812. }
  813. if (select_identifiers_enabled && highlighted_word != String()) {
  814. if (highlighted_word == range) {
  815. underlined = true;
  816. }
  817. }
  818. }
  819. if (!in_function_name && in_word && !in_keyword) {
  820. int k = j;
  821. while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  822. k++;
  823. }
  824. // check for space between name and bracket
  825. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  826. k++;
  827. }
  828. if (str[k] == '(') {
  829. in_function_name = true;
  830. }
  831. }
  832. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  833. int k = j;
  834. while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  835. k--;
  836. }
  837. if (str[k] == '.') {
  838. in_member_variable = true;
  839. }
  840. }
  841. if (is_symbol) {
  842. in_function_name = false;
  843. in_member_variable = false;
  844. }
  845. if (in_region >= 0)
  846. color = color_regions[in_region].color;
  847. else if (in_keyword)
  848. color = keyword_color;
  849. else if (in_member_variable)
  850. color = cache.member_variable_color;
  851. else if (in_function_name)
  852. color = cache.function_color;
  853. else if (is_symbol)
  854. color = cache.symbol_color;
  855. else if (is_number)
  856. color = cache.number_color;
  857. prev_is_char = is_char;
  858. prev_is_number = is_number;
  859. }
  860. int char_w;
  861. //handle tabulator
  862. if (str[j] == '\t') {
  863. int left = char_ofs % tab_w;
  864. if (left == 0)
  865. char_w = tab_w;
  866. else
  867. char_w = tab_w - char_ofs % tab_w; // is right...
  868. } else {
  869. char_w = cache.font->get_char_size(str[j], str[j + 1]).width;
  870. }
  871. if ((char_ofs + char_margin) < xmargin_beg) {
  872. char_ofs += char_w;
  873. continue;
  874. }
  875. if ((char_ofs + char_margin + char_w) >= xmargin_end) {
  876. if (syntax_coloring)
  877. continue;
  878. else
  879. break;
  880. }
  881. bool in_search_result = false;
  882. if (search_text_col != -1) {
  883. // if we are at the end check for new search result on same line
  884. if (j >= search_text_col + search_text.length())
  885. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, j);
  886. in_search_result = j >= search_text_col && j < search_text_col + search_text.length();
  887. if (in_search_result) {
  888. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, get_row_height())), cache.search_result_color);
  889. }
  890. }
  891. //current line highlighting
  892. bool in_selection = (selection.active && line >= selection.from_line && line <= selection.to_line && (line > selection.from_line || j >= selection.from_column) && (line < selection.to_line || j < selection.to_column));
  893. if (line == cursor.line && highlight_current_line) {
  894. // if its the last char draw to end of the line
  895. if (j == str.length() - 1) {
  896. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(char_ofs + char_margin + char_w, ofs_y, xmargin_end - (char_ofs + char_margin + char_w), get_row_height()), cache.current_line_color);
  897. }
  898. // actual text
  899. if (!in_selection) {
  900. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, get_row_height())), cache.current_line_color);
  901. }
  902. }
  903. if (in_selection) {
  904. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, get_row_height())), cache.selection_color);
  905. }
  906. if (in_search_result) {
  907. Color border_color = (line == search_result_line && j >= search_result_col && j < search_result_col + search_text.length()) ? cache.font_color : cache.search_result_border_color;
  908. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, 1)), border_color);
  909. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y + get_row_height() - 1), Size2i(char_w, 1)), border_color);
  910. if (j == search_text_col)
  911. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(1, get_row_height())), border_color);
  912. if (j == search_text_col + search_text.length() - 1)
  913. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + char_w + ofs_x - 1, ofs_y), Size2i(1, get_row_height())), border_color);
  914. }
  915. if (highlight_all_occurrences) {
  916. if (highlighted_text_col != -1) {
  917. // if we are at the end check for new word on same line
  918. if (j > highlighted_text_col + highlighted_text.length()) {
  919. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, j);
  920. }
  921. bool in_highlighted_word = (j >= highlighted_text_col && j < highlighted_text_col + highlighted_text.length());
  922. /* if this is the original highlighted text we don't want to highlight it again */
  923. if (cursor.line == line && (cursor.column >= highlighted_text_col && cursor.column <= highlighted_text_col + highlighted_text.length())) {
  924. in_highlighted_word = false;
  925. }
  926. if (in_highlighted_word) {
  927. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, get_row_height())), cache.word_highlighted_color);
  928. }
  929. }
  930. }
  931. if (brace_matching_enabled) {
  932. if ((brace_open_match_line == line && brace_open_match_column == j) ||
  933. (cursor.column == j && cursor.line == line && (brace_open_matching || brace_open_mismatch))) {
  934. if (brace_open_mismatch)
  935. color = cache.brace_mismatch_color;
  936. cache.font->draw_char(ci, Point2i(char_ofs + char_margin + ofs_x, ofs_y + ascent), '_', str[j + 1], in_selection && override_selected_font_color ? cache.font_selected_color : color);
  937. }
  938. if (
  939. (brace_close_match_line == line && brace_close_match_column == j) ||
  940. (cursor.column == j + 1 && cursor.line == line && (brace_close_matching || brace_close_mismatch))) {
  941. if (brace_close_mismatch)
  942. color = cache.brace_mismatch_color;
  943. cache.font->draw_char(ci, Point2i(char_ofs + char_margin + ofs_x, ofs_y + ascent), '_', str[j + 1], in_selection && override_selected_font_color ? cache.font_selected_color : color);
  944. }
  945. }
  946. if (cursor.column == j && cursor.line == line) {
  947. cursor_pos = Point2i(char_ofs + char_margin + ofs_x, ofs_y);
  948. if (insert_mode) {
  949. cursor_pos.y += (get_row_height() - 3);
  950. }
  951. int caret_w = (str[j] == '\t') ? cache.font->get_char_size(' ').width : char_w;
  952. if (ime_text.length() > 0) {
  953. int ofs = 0;
  954. while (true) {
  955. if (ofs >= ime_text.length())
  956. break;
  957. CharType cchar = ime_text[ofs];
  958. CharType next = ime_text[ofs + 1];
  959. int im_char_width = cache.font->get_char_size(cchar, next).width;
  960. if ((char_ofs + char_margin + im_char_width) >= xmargin_end)
  961. break;
  962. bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y;
  963. if (selected) {
  964. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_ofs + char_margin, ofs_y + get_row_height()), Size2(im_char_width, 3)), color);
  965. } else {
  966. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_ofs + char_margin, ofs_y + get_row_height()), Size2(im_char_width, 1)), color);
  967. }
  968. cache.font->draw_char(ci, Point2(char_ofs + char_margin + ofs_x, ofs_y + ascent), cchar, next, color);
  969. char_ofs += im_char_width;
  970. ofs++;
  971. }
  972. }
  973. if (ime_text.length() == 0) {
  974. if (draw_caret) {
  975. if (insert_mode) {
  976. int caret_h = (block_caret) ? 4 : 1;
  977. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, caret_h)), cache.caret_color);
  978. } else {
  979. caret_w = (block_caret) ? caret_w : 1;
  980. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, get_row_height())), cache.caret_color);
  981. }
  982. }
  983. }
  984. }
  985. if (cursor.column == j && cursor.line == line && block_caret && draw_caret && !insert_mode) {
  986. color = cache.caret_background_color;
  987. } else if (!syntax_coloring && block_caret) {
  988. color = cache.font_color;
  989. color.a *= readonly_alpha;
  990. }
  991. if (str[j] >= 32) {
  992. int w = cache.font->draw_char(ci, Point2i(char_ofs + char_margin + ofs_x, ofs_y + ascent), str[j], str[j + 1], in_selection && override_selected_font_color ? cache.font_selected_color : color);
  993. if (underlined) {
  994. draw_rect(Rect2(char_ofs + char_margin + ofs_x, ofs_y + ascent + 2, w, 1), in_selection && override_selected_font_color ? cache.font_selected_color : color);
  995. }
  996. }
  997. else if (draw_tabs && str[j] == '\t') {
  998. int yofs = (get_row_height() - cache.tab_icon->get_height()) / 2;
  999. cache.tab_icon->draw(ci, Point2(char_ofs + char_margin + ofs_x, ofs_y + yofs), in_selection && override_selected_font_color ? cache.font_selected_color : color);
  1000. }
  1001. char_ofs += char_w;
  1002. if (j == str.length() - 1 && is_folded(line)) {
  1003. int yofs = (get_row_height() - cache.folded_eol_icon->get_height()) / 2;
  1004. int xofs = cache.folded_eol_icon->get_width() / 2;
  1005. Color eol_color = cache.code_folding_color;
  1006. eol_color.a = 1;
  1007. cache.folded_eol_icon->draw(ci, Point2(char_ofs + char_margin + xofs + ofs_x, ofs_y + yofs), eol_color);
  1008. }
  1009. }
  1010. if (cursor.column == str.length() && cursor.line == line && (char_ofs + char_margin) >= xmargin_beg) {
  1011. cursor_pos = Point2i(char_ofs + char_margin + ofs_x, ofs_y);
  1012. if (insert_mode) {
  1013. cursor_pos.y += (get_row_height() - 3);
  1014. }
  1015. if (ime_text.length() > 0) {
  1016. int ofs = 0;
  1017. while (true) {
  1018. if (ofs >= ime_text.length())
  1019. break;
  1020. CharType cchar = ime_text[ofs];
  1021. CharType next = ime_text[ofs + 1];
  1022. int im_char_width = cache.font->get_char_size(cchar, next).width;
  1023. if ((char_ofs + char_margin + im_char_width) >= xmargin_end)
  1024. break;
  1025. bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y;
  1026. if (selected) {
  1027. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_ofs + char_margin, ofs_y + get_row_height()), Size2(im_char_width, 3)), color);
  1028. } else {
  1029. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_ofs + char_margin, ofs_y + get_row_height()), Size2(im_char_width, 1)), color);
  1030. }
  1031. cache.font->draw_char(ci, Point2(char_ofs + char_margin + ofs_x, ofs_y + ascent), cchar, next, color);
  1032. char_ofs += im_char_width;
  1033. ofs++;
  1034. }
  1035. }
  1036. if (ime_text.length() == 0) {
  1037. if (draw_caret) {
  1038. if (insert_mode) {
  1039. int char_w = cache.font->get_char_size(' ').width;
  1040. int caret_h = (block_caret) ? 4 : 1;
  1041. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(char_w, caret_h)), cache.caret_color);
  1042. } else {
  1043. int char_w = cache.font->get_char_size(' ').width;
  1044. int caret_w = (block_caret) ? char_w : 1;
  1045. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, get_row_height())), cache.caret_color);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. }
  1051. if (line_length_guideline) {
  1052. int x = xmargin_beg + cache.font->get_char_size('0').width * line_length_guideline_col - cursor.x_ofs;
  1053. if (x > xmargin_beg && x < xmargin_end) {
  1054. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(x, 0), Point2(x, cache.size.height), cache.line_length_guideline_color);
  1055. }
  1056. }
  1057. bool completion_below = false;
  1058. if (completion_active) {
  1059. // code completion box
  1060. Ref<StyleBox> csb = get_stylebox("completion");
  1061. int maxlines = get_constant("completion_lines");
  1062. int cmax_width = get_constant("completion_max_width") * cache.font->get_char_size('x').x;
  1063. int scrollw = get_constant("completion_scroll_width");
  1064. Color scrollc = get_color("completion_scroll_color");
  1065. int lines = MIN(completion_options.size(), maxlines);
  1066. int w = 0;
  1067. int h = lines * get_row_height();
  1068. int nofs = cache.font->get_string_size(completion_base).width;
  1069. if (completion_options.size() < 50) {
  1070. for (int i = 0; i < completion_options.size(); i++) {
  1071. int w2 = MIN(cache.font->get_string_size(completion_options[i]).x, cmax_width);
  1072. if (w2 > w)
  1073. w = w2;
  1074. }
  1075. } else {
  1076. w = cmax_width;
  1077. }
  1078. int th = h + csb->get_minimum_size().y;
  1079. if (cursor_pos.y + get_row_height() + th > get_size().height) {
  1080. completion_rect.position.y = cursor_pos.y - th;
  1081. } else {
  1082. completion_rect.position.y = cursor_pos.y + get_row_height() + csb->get_offset().y;
  1083. completion_below = true;
  1084. }
  1085. if (cursor_pos.x - nofs + w + scrollw > get_size().width) {
  1086. completion_rect.position.x = get_size().width - w - scrollw;
  1087. } else {
  1088. completion_rect.position.x = cursor_pos.x - nofs;
  1089. }
  1090. completion_rect.size.width = w + 2;
  1091. completion_rect.size.height = h;
  1092. if (completion_options.size() <= maxlines)
  1093. scrollw = 0;
  1094. draw_style_box(csb, Rect2(completion_rect.position - csb->get_offset(), completion_rect.size + csb->get_minimum_size() + Size2(scrollw, 0)));
  1095. if (cache.completion_background_color.a > 0.01) {
  1096. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(completion_rect.position, completion_rect.size + Size2(scrollw, 0)), cache.completion_background_color);
  1097. }
  1098. int line_from = CLAMP(completion_index - lines / 2, 0, completion_options.size() - lines);
  1099. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(completion_rect.position.x, completion_rect.position.y + (completion_index - line_from) * get_row_height()), Size2(completion_rect.size.width, get_row_height())), cache.completion_selected_color);
  1100. draw_rect(Rect2(completion_rect.position, Size2(nofs, completion_rect.size.height)), cache.completion_existing_color);
  1101. for (int i = 0; i < lines; i++) {
  1102. int l = line_from + i;
  1103. ERR_CONTINUE(l < 0 || l >= completion_options.size());
  1104. Color text_color = cache.completion_font_color;
  1105. for (int j = 0; j < color_regions.size(); j++) {
  1106. if (completion_options[l].begins_with(color_regions[j].begin_key)) {
  1107. text_color = color_regions[j].color;
  1108. }
  1109. }
  1110. draw_string(cache.font, Point2(completion_rect.position.x, completion_rect.position.y + i * get_row_height() + cache.font->get_ascent()), completion_options[l], text_color, completion_rect.size.width);
  1111. }
  1112. if (scrollw) {
  1113. //draw a small scroll rectangle to show a position in the options
  1114. float r = maxlines / (float)completion_options.size();
  1115. float o = line_from / (float)completion_options.size();
  1116. draw_rect(Rect2(completion_rect.position.x + completion_rect.size.width, completion_rect.position.y + o * completion_rect.size.y, scrollw, completion_rect.size.y * r), scrollc);
  1117. }
  1118. completion_line_ofs = line_from;
  1119. }
  1120. // check to see if the hint should be drawn
  1121. bool show_hint = false;
  1122. if (completion_hint != "") {
  1123. if (completion_active) {
  1124. if (completion_below && !callhint_below) {
  1125. show_hint = true;
  1126. } else if (!completion_below && callhint_below) {
  1127. show_hint = true;
  1128. }
  1129. } else {
  1130. show_hint = true;
  1131. }
  1132. }
  1133. if (show_hint) {
  1134. Ref<StyleBox> sb = get_stylebox("panel", "TooltipPanel");
  1135. Ref<Font> font = cache.font;
  1136. Color font_color = get_color("font_color", "TooltipLabel");
  1137. int max_w = 0;
  1138. int sc = completion_hint.get_slice_count("\n");
  1139. int offset = 0;
  1140. int spacing = 0;
  1141. for (int i = 0; i < sc; i++) {
  1142. String l = completion_hint.get_slice("\n", i);
  1143. int len = font->get_string_size(l).x;
  1144. max_w = MAX(len, max_w);
  1145. if (i == 0) {
  1146. offset = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF)))).x;
  1147. } else {
  1148. spacing += cache.line_spacing;
  1149. }
  1150. }
  1151. Size2 size = Size2(max_w, sc * font->get_height() + spacing);
  1152. Size2 minsize = size + sb->get_minimum_size();
  1153. if (completion_hint_offset == -0xFFFF) {
  1154. completion_hint_offset = cursor_pos.x - offset;
  1155. }
  1156. Point2 hint_ofs = Vector2(completion_hint_offset, cursor_pos.y) + callhint_offset;
  1157. if (callhint_below) {
  1158. hint_ofs.y += get_row_height() + sb->get_offset().y;
  1159. } else {
  1160. hint_ofs.y -= minsize.y + sb->get_offset().y;
  1161. }
  1162. draw_style_box(sb, Rect2(hint_ofs, minsize));
  1163. spacing = 0;
  1164. for (int i = 0; i < sc; i++) {
  1165. int begin = 0;
  1166. int end = 0;
  1167. String l = completion_hint.get_slice("\n", i);
  1168. if (l.find(String::chr(0xFFFF)) != -1) {
  1169. begin = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF)))).x;
  1170. end = font->get_string_size(l.substr(0, l.rfind(String::chr(0xFFFF)))).x;
  1171. }
  1172. draw_string(font, hint_ofs + sb->get_offset() + Vector2(0, font->get_ascent() + font->get_height() * i + spacing), l.replace(String::chr(0xFFFF), ""), font_color);
  1173. if (end > 0) {
  1174. Vector2 b = hint_ofs + sb->get_offset() + Vector2(begin, font->get_height() + font->get_height() * i + spacing - 1);
  1175. draw_line(b, b + Vector2(end - begin, 0), font_color);
  1176. }
  1177. spacing += cache.line_spacing;
  1178. }
  1179. }
  1180. if (has_focus()) {
  1181. OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos + Point2(0, get_row_height()));
  1182. OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this);
  1183. }
  1184. } break;
  1185. case NOTIFICATION_FOCUS_ENTER: {
  1186. if (!caret_blink_enabled) {
  1187. draw_caret = true;
  1188. }
  1189. Point2 cursor_pos = Point2(cursor_get_column(), cursor_get_line()) * get_row_height();
  1190. OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos);
  1191. OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this);
  1192. if (OS::get_singleton()->has_virtual_keyboard())
  1193. OS::get_singleton()->show_virtual_keyboard(get_text(), get_global_rect());
  1194. if (raised_from_completion) {
  1195. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
  1196. }
  1197. } break;
  1198. case NOTIFICATION_FOCUS_EXIT: {
  1199. OS::get_singleton()->set_ime_position(Point2());
  1200. OS::get_singleton()->set_ime_intermediate_text_callback(NULL, NULL);
  1201. ime_text = "";
  1202. ime_selection = Point2();
  1203. if (OS::get_singleton()->has_virtual_keyboard())
  1204. OS::get_singleton()->hide_virtual_keyboard();
  1205. if (raised_from_completion) {
  1206. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
  1207. }
  1208. } break;
  1209. }
  1210. }
  1211. void TextEdit::_ime_text_callback(void *p_self, String p_text, Point2 p_selection) {
  1212. TextEdit *self = (TextEdit *)p_self;
  1213. self->ime_text = p_text;
  1214. self->ime_selection = p_selection;
  1215. self->update();
  1216. }
  1217. void TextEdit::_consume_pair_symbol(CharType ch) {
  1218. int cursor_position_to_move = cursor_get_column() + 1;
  1219. CharType ch_single[2] = { ch, 0 };
  1220. CharType ch_single_pair[2] = { _get_right_pair_symbol(ch), 0 };
  1221. CharType ch_pair[3] = { ch, _get_right_pair_symbol(ch), 0 };
  1222. if (is_selection_active()) {
  1223. int new_column, new_line;
  1224. begin_complex_operation();
  1225. _insert_text(get_selection_from_line(), get_selection_from_column(),
  1226. ch_single,
  1227. &new_line, &new_column);
  1228. int to_col_offset = 0;
  1229. if (get_selection_from_line() == get_selection_to_line())
  1230. to_col_offset = 1;
  1231. _insert_text(get_selection_to_line(),
  1232. get_selection_to_column() + to_col_offset,
  1233. ch_single_pair,
  1234. &new_line, &new_column);
  1235. end_complex_operation();
  1236. cursor_set_line(get_selection_to_line());
  1237. cursor_set_column(get_selection_to_column() + to_col_offset);
  1238. deselect();
  1239. update();
  1240. return;
  1241. }
  1242. if ((ch == '\'' || ch == '"') &&
  1243. cursor_get_column() > 0 &&
  1244. _is_text_char(text[cursor.line][cursor_get_column() - 1])) {
  1245. insert_text_at_cursor(ch_single);
  1246. cursor_set_column(cursor_position_to_move);
  1247. return;
  1248. }
  1249. if (cursor_get_column() < text[cursor.line].length()) {
  1250. if (_is_text_char(text[cursor.line][cursor_get_column()])) {
  1251. insert_text_at_cursor(ch_single);
  1252. cursor_set_column(cursor_position_to_move);
  1253. return;
  1254. }
  1255. if (_is_pair_right_symbol(ch) &&
  1256. text[cursor.line][cursor_get_column()] == ch) {
  1257. cursor_set_column(cursor_position_to_move);
  1258. return;
  1259. }
  1260. }
  1261. insert_text_at_cursor(ch_pair);
  1262. cursor_set_column(cursor_position_to_move);
  1263. return;
  1264. }
  1265. void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
  1266. bool remove_right_symbol = false;
  1267. if (cursor.column < text[cursor.line].length() && cursor.column > 0) {
  1268. CharType left_char = text[cursor.line][cursor.column - 1];
  1269. CharType right_char = text[cursor.line][cursor.column];
  1270. if (right_char == _get_right_pair_symbol(left_char)) {
  1271. remove_right_symbol = true;
  1272. }
  1273. }
  1274. if (remove_right_symbol) {
  1275. _remove_text(prev_line, prev_column, cursor.line, cursor.column + 1);
  1276. } else {
  1277. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1278. }
  1279. }
  1280. void TextEdit::backspace_at_cursor() {
  1281. if (readonly)
  1282. return;
  1283. if (cursor.column == 0 && cursor.line == 0)
  1284. return;
  1285. int prev_line = cursor.column ? cursor.line : cursor.line - 1;
  1286. int prev_column = cursor.column ? (cursor.column - 1) : (text[cursor.line - 1].length());
  1287. if (is_line_hidden(cursor.line))
  1288. set_line_as_hidden(prev_line, true);
  1289. if (is_line_set_as_breakpoint(cursor.line))
  1290. set_line_as_breakpoint(prev_line, true);
  1291. if (auto_brace_completion_enabled &&
  1292. cursor.column > 0 &&
  1293. _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
  1294. _consume_backspace_for_pair_symbol(prev_line, prev_column);
  1295. } else {
  1296. // handle space indentation
  1297. if (cursor.column - indent_size >= 0 && indent_using_spaces) {
  1298. // if there is enough spaces to count as a tab
  1299. bool unindent = true;
  1300. for (int i = 1; i <= indent_size; i++) {
  1301. if (text[cursor.line][cursor.column - i] != ' ') {
  1302. unindent = false;
  1303. break;
  1304. }
  1305. }
  1306. // and it is before the first character
  1307. int i = 0;
  1308. while (i < cursor.column && i < text[cursor.line].length()) {
  1309. if (text[cursor.line][i] != ' ' && text[cursor.line][i] != '\t') {
  1310. unindent = false;
  1311. break;
  1312. }
  1313. i++;
  1314. }
  1315. // then we can remove it as a single character.
  1316. if (unindent) {
  1317. _remove_text(cursor.line, cursor.column - indent_size, cursor.line, cursor.column);
  1318. prev_column = cursor.column - indent_size;
  1319. } else {
  1320. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1321. }
  1322. } else {
  1323. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1324. }
  1325. }
  1326. cursor_set_line(prev_line, true, true);
  1327. cursor_set_column(prev_column);
  1328. }
  1329. void TextEdit::indent_right() {
  1330. int start_line;
  1331. int end_line;
  1332. begin_complex_operation();
  1333. if (is_selection_active()) {
  1334. start_line = get_selection_from_line();
  1335. end_line = get_selection_to_line();
  1336. } else {
  1337. start_line = cursor.line;
  1338. end_line = start_line;
  1339. }
  1340. // ignore if the cursor is not past the first column
  1341. if (is_selection_active() && get_selection_to_column() == 0) {
  1342. end_line--;
  1343. }
  1344. for (int i = start_line; i <= end_line; i++) {
  1345. String line_text = get_line(i);
  1346. if (indent_using_spaces) {
  1347. line_text = space_indent + line_text;
  1348. } else {
  1349. line_text = '\t' + line_text;
  1350. }
  1351. set_line(i, line_text);
  1352. }
  1353. // fix selection and cursor being off by one on the last line
  1354. if (is_selection_active()) {
  1355. selection.to_column++;
  1356. selection.from_column++;
  1357. }
  1358. cursor.column++;
  1359. end_complex_operation();
  1360. update();
  1361. }
  1362. void TextEdit::indent_left() {
  1363. int start_line;
  1364. int end_line;
  1365. begin_complex_operation();
  1366. if (is_selection_active()) {
  1367. start_line = get_selection_from_line();
  1368. end_line = get_selection_to_line();
  1369. } else {
  1370. start_line = cursor.line;
  1371. end_line = start_line;
  1372. }
  1373. // ignore if the cursor is not past the first column
  1374. if (is_selection_active() && get_selection_to_column() == 0) {
  1375. end_line--;
  1376. }
  1377. String last_line_text = get_line(end_line);
  1378. for (int i = start_line; i <= end_line; i++) {
  1379. String line_text = get_line(i);
  1380. if (line_text.begins_with("\t")) {
  1381. line_text = line_text.substr(1, line_text.length());
  1382. set_line(i, line_text);
  1383. } else if (line_text.begins_with(space_indent)) {
  1384. line_text = line_text.substr(indent_size, line_text.length());
  1385. set_line(i, line_text);
  1386. }
  1387. }
  1388. // fix selection and cursor being off by one on the last line
  1389. if (is_selection_active() && last_line_text != get_line(end_line)) {
  1390. if (selection.to_column > 0)
  1391. selection.to_column--;
  1392. if (selection.from_column > 0)
  1393. selection.from_column--;
  1394. }
  1395. if (cursor.column > 0) {
  1396. cursor.column--;
  1397. }
  1398. end_complex_operation();
  1399. update();
  1400. }
  1401. void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const {
  1402. float rows = p_mouse.y;
  1403. rows -= cache.style_normal->get_margin(MARGIN_TOP);
  1404. rows /= get_row_height();
  1405. int lsp = get_line_scroll_pos(true);
  1406. int row = cursor.line_ofs + (rows + (round(v_scroll->get_value()) - lsp));
  1407. if (is_hiding_enabled()) {
  1408. // row will be offset by the hidden rows
  1409. int f_ofs = num_lines_from(CLAMP(cursor.line_ofs, 0, text.size() - 1), MIN(rows + 1, text.size() - cursor.line_ofs)) - 1;
  1410. row = cursor.line_ofs + (f_ofs + (round(v_scroll->get_value()) - lsp));
  1411. row = CLAMP(row, 0, text.size() - num_lines_from(text.size() - 1, -1));
  1412. }
  1413. if (row < 0)
  1414. row = 0; //todo
  1415. int col = 0;
  1416. if (row >= text.size()) {
  1417. row = text.size() - 1;
  1418. col = text[row].size();
  1419. } else {
  1420. col = p_mouse.x - (cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width);
  1421. col += cursor.x_ofs;
  1422. col = get_char_pos_for(col, get_line(row));
  1423. }
  1424. r_row = row;
  1425. r_col = col;
  1426. }
  1427. void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
  1428. Ref<InputEventMouseButton> mb = p_gui_input;
  1429. if (mb.is_valid()) {
  1430. if (completion_active && completion_rect.has_point(mb->get_position())) {
  1431. if (!mb->is_pressed())
  1432. return;
  1433. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  1434. if (completion_index > 0) {
  1435. completion_index--;
  1436. completion_current = completion_options[completion_index];
  1437. update();
  1438. }
  1439. }
  1440. if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  1441. if (completion_index < completion_options.size() - 1) {
  1442. completion_index++;
  1443. completion_current = completion_options[completion_index];
  1444. update();
  1445. }
  1446. }
  1447. if (mb->get_button_index() == BUTTON_LEFT) {
  1448. completion_index = CLAMP(completion_line_ofs + (mb->get_position().y - completion_rect.position.y) / get_row_height(), 0, completion_options.size() - 1);
  1449. completion_current = completion_options[completion_index];
  1450. update();
  1451. if (mb->is_doubleclick())
  1452. _confirm_completion();
  1453. }
  1454. return;
  1455. } else {
  1456. _cancel_completion();
  1457. _cancel_code_hint();
  1458. }
  1459. if (mb->is_pressed()) {
  1460. if (mb->get_button_index() == BUTTON_WHEEL_UP && !mb->get_command()) {
  1461. _scroll_up(3 * mb->get_factor());
  1462. }
  1463. if (mb->get_button_index() == BUTTON_WHEEL_DOWN && !mb->get_command()) {
  1464. _scroll_down(3 * mb->get_factor());
  1465. }
  1466. if (mb->get_button_index() == BUTTON_WHEEL_LEFT) {
  1467. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1468. }
  1469. if (mb->get_button_index() == BUTTON_WHEEL_RIGHT) {
  1470. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1471. }
  1472. if (mb->get_button_index() == BUTTON_LEFT) {
  1473. _reset_caret_blink_timer();
  1474. int row, col;
  1475. update_line_scroll_pos();
  1476. _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
  1477. if (mb->get_command() && highlighted_word != String()) {
  1478. emit_signal("symbol_lookup", highlighted_word, row, col);
  1479. return;
  1480. }
  1481. // toggle breakpoint on gutter click
  1482. if (draw_breakpoint_gutter) {
  1483. int gutter = cache.style_normal->get_margin(MARGIN_LEFT);
  1484. if (mb->get_position().x > gutter && mb->get_position().x <= gutter + cache.breakpoint_gutter_width + 3) {
  1485. set_line_as_breakpoint(row, !is_line_set_as_breakpoint(row));
  1486. emit_signal("breakpoint_toggled", row);
  1487. return;
  1488. }
  1489. }
  1490. // toggle fold on gutter click if can
  1491. if (draw_fold_gutter) {
  1492. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  1493. int gutter_left = left_margin + cache.breakpoint_gutter_width + cache.line_number_w;
  1494. if (mb->get_position().x > gutter_left - 6 && mb->get_position().x <= gutter_left + cache.fold_gutter_width - 3) {
  1495. if (is_folded(row)) {
  1496. unfold_line(row);
  1497. } else if (can_fold(row)) {
  1498. fold_line(row);
  1499. }
  1500. return;
  1501. }
  1502. }
  1503. // unfold on folded icon click
  1504. if (is_folded(row)) {
  1505. int line_width = text.get_line_width(row);
  1506. line_width += cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width - cursor.x_ofs;
  1507. if (mb->get_position().x > line_width - 3 && mb->get_position().x <= line_width + cache.folded_eol_icon->get_width() + 3) {
  1508. unfold_line(row);
  1509. return;
  1510. }
  1511. }
  1512. int prev_col = cursor.column;
  1513. int prev_line = cursor.line;
  1514. cursor_set_line(row, true, false);
  1515. cursor_set_column(col);
  1516. if (mb->get_shift() && (cursor.column != prev_col || cursor.line != prev_line)) {
  1517. if (!selection.active) {
  1518. selection.active = true;
  1519. selection.selecting_mode = Selection::MODE_POINTER;
  1520. selection.from_column = prev_col;
  1521. selection.from_line = prev_line;
  1522. selection.to_column = cursor.column;
  1523. selection.to_line = cursor.line;
  1524. if (selection.from_line > selection.to_line || (selection.from_line == selection.to_line && selection.from_column > selection.to_column)) {
  1525. SWAP(selection.from_column, selection.to_column);
  1526. SWAP(selection.from_line, selection.to_line);
  1527. selection.shiftclick_left = false;
  1528. } else {
  1529. selection.shiftclick_left = true;
  1530. }
  1531. selection.selecting_line = prev_line;
  1532. selection.selecting_column = prev_col;
  1533. update();
  1534. } else {
  1535. if (cursor.line < selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column < selection.selecting_column)) {
  1536. if (selection.shiftclick_left) {
  1537. SWAP(selection.from_column, selection.to_column);
  1538. SWAP(selection.from_line, selection.to_line);
  1539. selection.shiftclick_left = !selection.shiftclick_left;
  1540. }
  1541. selection.from_column = cursor.column;
  1542. selection.from_line = cursor.line;
  1543. } else if (cursor.line > selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column > selection.selecting_column)) {
  1544. if (!selection.shiftclick_left) {
  1545. SWAP(selection.from_column, selection.to_column);
  1546. SWAP(selection.from_line, selection.to_line);
  1547. selection.shiftclick_left = !selection.shiftclick_left;
  1548. }
  1549. selection.to_column = cursor.column;
  1550. selection.to_line = cursor.line;
  1551. } else {
  1552. selection.active = false;
  1553. }
  1554. update();
  1555. }
  1556. } else {
  1557. //if sel active and dblick last time < something
  1558. //else
  1559. selection.active = false;
  1560. selection.selecting_mode = Selection::MODE_POINTER;
  1561. selection.selecting_line = row;
  1562. selection.selecting_column = col;
  1563. }
  1564. if (!mb->is_doubleclick() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < 600 && cursor.line == prev_line) {
  1565. //tripleclick select line
  1566. selection.selecting_mode = Selection::MODE_LINE;
  1567. _update_selection_mode_line();
  1568. last_dblclk = 0;
  1569. } else if (mb->is_doubleclick() && text[cursor.line].length()) {
  1570. //doubleclick select world
  1571. selection.selecting_mode = Selection::MODE_WORD;
  1572. _update_selection_mode_word();
  1573. last_dblclk = OS::get_singleton()->get_ticks_msec();
  1574. }
  1575. update();
  1576. }
  1577. if (mb->get_button_index() == BUTTON_RIGHT && context_menu_enabled) {
  1578. menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  1579. menu->set_size(Vector2(1, 1));
  1580. menu->popup();
  1581. grab_focus();
  1582. }
  1583. } else {
  1584. if (mb->get_button_index() == BUTTON_LEFT)
  1585. click_select_held->stop();
  1586. // notify to show soft keyboard
  1587. notification(NOTIFICATION_FOCUS_ENTER);
  1588. }
  1589. }
  1590. const Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  1591. if (pan_gesture.is_valid()) {
  1592. const real_t delta = pan_gesture->get_delta().y;
  1593. if (delta < 0) {
  1594. _scroll_up(-delta);
  1595. } else {
  1596. _scroll_down(delta);
  1597. }
  1598. h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
  1599. return;
  1600. }
  1601. Ref<InputEventMouseMotion> mm = p_gui_input;
  1602. if (mm.is_valid()) {
  1603. if (select_identifiers_enabled) {
  1604. if (mm->get_command() && mm->get_button_mask() == 0) {
  1605. String new_word = get_word_at_pos(mm->get_position());
  1606. if (new_word != highlighted_word) {
  1607. highlighted_word = new_word;
  1608. update();
  1609. }
  1610. } else {
  1611. if (highlighted_word != String()) {
  1612. highlighted_word = String();
  1613. update();
  1614. }
  1615. }
  1616. }
  1617. if (mm->get_button_mask() & BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { //ignore if dragging
  1618. _reset_caret_blink_timer();
  1619. switch (selection.selecting_mode) {
  1620. case Selection::MODE_POINTER: {
  1621. _update_selection_mode_pointer();
  1622. } break;
  1623. case Selection::MODE_WORD: {
  1624. _update_selection_mode_word();
  1625. } break;
  1626. case Selection::MODE_LINE: {
  1627. _update_selection_mode_line();
  1628. } break;
  1629. default: {
  1630. break;
  1631. }
  1632. }
  1633. }
  1634. }
  1635. Ref<InputEventKey> k = p_gui_input;
  1636. if (k.is_valid()) {
  1637. k = k->duplicate(); //it will be modified later on
  1638. #ifdef OSX_ENABLED
  1639. if (k->get_scancode() == KEY_META) {
  1640. #else
  1641. if (k->get_scancode() == KEY_CONTROL) {
  1642. #endif
  1643. if (select_identifiers_enabled) {
  1644. if (k->is_pressed()) {
  1645. highlighted_word = get_word_at_pos(get_local_mouse_position());
  1646. update();
  1647. } else {
  1648. highlighted_word = String();
  1649. update();
  1650. }
  1651. }
  1652. }
  1653. if (!k->is_pressed())
  1654. return;
  1655. if (completion_active) {
  1656. if (readonly)
  1657. return;
  1658. bool valid = true;
  1659. if (k->get_command() || k->get_metakey())
  1660. valid = false;
  1661. if (valid) {
  1662. if (!k->get_alt()) {
  1663. if (k->get_scancode() == KEY_UP) {
  1664. if (completion_index > 0) {
  1665. completion_index--;
  1666. completion_current = completion_options[completion_index];
  1667. update();
  1668. }
  1669. accept_event();
  1670. return;
  1671. }
  1672. if (k->get_scancode() == KEY_DOWN) {
  1673. if (completion_index < completion_options.size() - 1) {
  1674. completion_index++;
  1675. completion_current = completion_options[completion_index];
  1676. update();
  1677. }
  1678. accept_event();
  1679. return;
  1680. }
  1681. if (k->get_scancode() == KEY_PAGEUP) {
  1682. completion_index -= get_constant("completion_lines");
  1683. if (completion_index < 0)
  1684. completion_index = 0;
  1685. completion_current = completion_options[completion_index];
  1686. update();
  1687. accept_event();
  1688. return;
  1689. }
  1690. if (k->get_scancode() == KEY_PAGEDOWN) {
  1691. completion_index += get_constant("completion_lines");
  1692. if (completion_index >= completion_options.size())
  1693. completion_index = completion_options.size() - 1;
  1694. completion_current = completion_options[completion_index];
  1695. update();
  1696. accept_event();
  1697. return;
  1698. }
  1699. if (k->get_scancode() == KEY_HOME && completion_index > 0) {
  1700. completion_index = 0;
  1701. completion_current = completion_options[completion_index];
  1702. update();
  1703. accept_event();
  1704. return;
  1705. }
  1706. if (k->get_scancode() == KEY_END && completion_index < completion_options.size() - 1) {
  1707. completion_index = completion_options.size() - 1;
  1708. completion_current = completion_options[completion_index];
  1709. update();
  1710. accept_event();
  1711. return;
  1712. }
  1713. if (k->get_scancode() == KEY_KP_ENTER || k->get_scancode() == KEY_ENTER || k->get_scancode() == KEY_TAB) {
  1714. _confirm_completion();
  1715. accept_event();
  1716. return;
  1717. }
  1718. if (k->get_scancode() == KEY_BACKSPACE) {
  1719. _reset_caret_blink_timer();
  1720. backspace_at_cursor();
  1721. _update_completion_candidates();
  1722. accept_event();
  1723. return;
  1724. }
  1725. if (k->get_scancode() == KEY_SHIFT) {
  1726. accept_event();
  1727. return;
  1728. }
  1729. }
  1730. if (k->get_unicode() > 32) {
  1731. _reset_caret_blink_timer();
  1732. const CharType chr[2] = { (CharType)k->get_unicode(), 0 };
  1733. if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  1734. _consume_pair_symbol(chr[0]);
  1735. } else {
  1736. // remove the old character if in insert mode
  1737. if (insert_mode) {
  1738. begin_complex_operation();
  1739. // make sure we don't try and remove empty space
  1740. if (cursor.column < get_line(cursor.line).length()) {
  1741. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  1742. }
  1743. }
  1744. _insert_text_at_cursor(chr);
  1745. if (insert_mode) {
  1746. end_complex_operation();
  1747. }
  1748. }
  1749. _update_completion_candidates();
  1750. accept_event();
  1751. return;
  1752. }
  1753. }
  1754. _cancel_completion();
  1755. }
  1756. /* TEST CONTROL FIRST!! */
  1757. // some remaps for duplicate functions..
  1758. if (k->get_command() && !k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_scancode() == KEY_INSERT) {
  1759. k->set_scancode(KEY_C);
  1760. }
  1761. if (!k->get_command() && k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_scancode() == KEY_INSERT) {
  1762. k->set_scancode(KEY_V);
  1763. k->set_command(true);
  1764. k->set_shift(false);
  1765. }
  1766. if (!k->get_command()) {
  1767. _reset_caret_blink_timer();
  1768. }
  1769. // save here for insert mode, just in case it is cleared in the following section
  1770. bool had_selection = selection.active;
  1771. // stuff to do when selection is active..
  1772. if (selection.active) {
  1773. if (readonly)
  1774. return;
  1775. bool clear = false;
  1776. bool unselect = false;
  1777. bool dobreak = false;
  1778. switch (k->get_scancode()) {
  1779. case KEY_TAB: {
  1780. if (k->get_shift()) {
  1781. indent_left();
  1782. } else {
  1783. indent_right();
  1784. }
  1785. dobreak = true;
  1786. accept_event();
  1787. } break;
  1788. case KEY_X:
  1789. case KEY_C:
  1790. //special keys often used with control, wait...
  1791. clear = (!k->get_command() || k->get_shift() || k->get_alt());
  1792. break;
  1793. case KEY_DELETE:
  1794. if (!k->get_shift()) {
  1795. accept_event();
  1796. clear = true;
  1797. dobreak = true;
  1798. } else if (k->get_command() || k->get_alt()) {
  1799. dobreak = true;
  1800. }
  1801. break;
  1802. case KEY_BACKSPACE:
  1803. accept_event();
  1804. clear = true;
  1805. dobreak = true;
  1806. break;
  1807. case KEY_LEFT:
  1808. case KEY_RIGHT:
  1809. case KEY_UP:
  1810. case KEY_DOWN:
  1811. case KEY_PAGEUP:
  1812. case KEY_PAGEDOWN:
  1813. case KEY_HOME:
  1814. case KEY_END:
  1815. // ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys)
  1816. if (k->get_command() || k->get_shift() || k->get_alt())
  1817. break;
  1818. unselect = true;
  1819. break;
  1820. default:
  1821. if (k->get_unicode() >= 32 && !k->get_command() && !k->get_alt() && !k->get_metakey())
  1822. clear = true;
  1823. if (auto_brace_completion_enabled && _is_pair_left_symbol(k->get_unicode()))
  1824. clear = false;
  1825. }
  1826. if (unselect) {
  1827. selection.active = false;
  1828. selection.selecting_mode = Selection::MODE_NONE;
  1829. update();
  1830. }
  1831. if (clear) {
  1832. if (!dobreak) {
  1833. begin_complex_operation();
  1834. }
  1835. selection.active = false;
  1836. update();
  1837. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  1838. cursor_set_line(selection.from_line, true, false);
  1839. cursor_set_column(selection.from_column);
  1840. update();
  1841. }
  1842. if (dobreak)
  1843. return;
  1844. }
  1845. selection.selecting_text = false;
  1846. bool scancode_handled = true;
  1847. // special scancode test...
  1848. switch (k->get_scancode()) {
  1849. case KEY_KP_ENTER:
  1850. case KEY_ENTER: {
  1851. if (readonly)
  1852. break;
  1853. String ins = "\n";
  1854. //keep indentation
  1855. int space_count = 0;
  1856. for (int i = 0; i < cursor.column; i++) {
  1857. if (text[cursor.line][i] == '\t') {
  1858. if (indent_using_spaces) {
  1859. ins += space_indent;
  1860. } else {
  1861. ins += "\t";
  1862. }
  1863. space_count = 0;
  1864. } else if (text[cursor.line][i] == ' ') {
  1865. space_count++;
  1866. if (space_count == indent_size) {
  1867. if (indent_using_spaces) {
  1868. ins += space_indent;
  1869. } else {
  1870. ins += "\t";
  1871. }
  1872. space_count = 0;
  1873. }
  1874. } else {
  1875. break;
  1876. }
  1877. }
  1878. if (is_folded(cursor.line))
  1879. unfold_line(cursor.line);
  1880. bool brace_indent = false;
  1881. // no need to indent if we are going upwards.
  1882. if (auto_indent && !(k->get_command() && k->get_shift())) {
  1883. // indent once again if previous line will end with ':' or '{'
  1884. // (i.e. colon/brace precedes current cursor position)
  1885. if (cursor.column > 0 && (text[cursor.line][cursor.column - 1] == ':' || text[cursor.line][cursor.column - 1] == '{')) {
  1886. if (indent_using_spaces) {
  1887. ins += space_indent;
  1888. } else {
  1889. ins += "\t";
  1890. }
  1891. // no need to move the brace below if we are not taking the text with us.
  1892. if (text[cursor.line][cursor.column] == '}' && !k->get_command()) {
  1893. brace_indent = true;
  1894. ins += "\n" + ins.substr(1, ins.length() - 2);
  1895. }
  1896. }
  1897. }
  1898. begin_complex_operation();
  1899. bool first_line = false;
  1900. if (k->get_command()) {
  1901. if (k->get_shift()) {
  1902. if (cursor.line > 0) {
  1903. cursor_set_line(cursor.line - 1);
  1904. cursor_set_column(text[cursor.line].length());
  1905. } else {
  1906. cursor_set_column(0);
  1907. first_line = true;
  1908. }
  1909. } else {
  1910. cursor_set_column(text[cursor.line].length());
  1911. }
  1912. }
  1913. insert_text_at_cursor(ins);
  1914. if (first_line) {
  1915. cursor_set_line(0);
  1916. } else if (brace_indent) {
  1917. cursor_set_line(cursor.line - 1);
  1918. cursor_set_column(text[cursor.line].length());
  1919. }
  1920. end_complex_operation();
  1921. } break;
  1922. case KEY_ESCAPE: {
  1923. if (completion_hint != "") {
  1924. completion_hint = "";
  1925. update();
  1926. } else {
  1927. scancode_handled = false;
  1928. }
  1929. } break;
  1930. case KEY_TAB: {
  1931. if (k->get_command()) break; // avoid tab when command
  1932. if (readonly)
  1933. break;
  1934. if (is_selection_active()) {
  1935. if (k->get_shift()) {
  1936. indent_left();
  1937. } else {
  1938. indent_right();
  1939. }
  1940. } else {
  1941. if (k->get_shift()) {
  1942. //simple unindent
  1943. int cc = cursor.column;
  1944. if (cc > 0 && cc <= text[cursor.line].length()) {
  1945. if (text[cursor.line][cursor.column - 1] == '\t') {
  1946. backspace_at_cursor();
  1947. } else {
  1948. if (cursor.column - indent_size >= 0) {
  1949. bool unindent = true;
  1950. for (int i = 1; i <= indent_size; i++) {
  1951. if (text[cursor.line][cursor.column - i] != ' ') {
  1952. unindent = false;
  1953. break;
  1954. }
  1955. }
  1956. if (unindent) {
  1957. _remove_text(cursor.line, cursor.column - indent_size, cursor.line, cursor.column);
  1958. cursor_set_column(cursor.column - indent_size);
  1959. }
  1960. }
  1961. }
  1962. }
  1963. } else {
  1964. //simple indent
  1965. if (indent_using_spaces) {
  1966. _insert_text_at_cursor(space_indent);
  1967. } else {
  1968. _insert_text_at_cursor("\t");
  1969. }
  1970. }
  1971. }
  1972. } break;
  1973. case KEY_BACKSPACE: {
  1974. if (readonly)
  1975. break;
  1976. #ifdef APPLE_STYLE_KEYS
  1977. if (k->get_alt() && cursor.column > 1) {
  1978. #else
  1979. if (k->get_alt()) {
  1980. scancode_handled = false;
  1981. break;
  1982. } else if (k->get_command() && cursor.column > 1) {
  1983. #endif
  1984. int line = cursor.line;
  1985. int column = cursor.column;
  1986. // check if we are removing a single whitespace, if so remove it and the next char type
  1987. // else we just remove the whitespace
  1988. bool only_whitespace = false;
  1989. if (_is_whitespace(text[line][column - 1]) && _is_whitespace(text[line][column - 2])) {
  1990. only_whitespace = true;
  1991. } else if (_is_whitespace(text[line][column - 1])) {
  1992. // remove the single whitespace
  1993. column--;
  1994. }
  1995. // check if its a text char
  1996. bool only_char = (_is_text_char(text[line][column - 1]) && !only_whitespace);
  1997. // if its not whitespace or char then symbol.
  1998. bool only_symbols = !(only_whitespace || only_char);
  1999. while (column > 0) {
  2000. bool is_whitespace = _is_whitespace(text[line][column - 1]);
  2001. bool is_text_char = _is_text_char(text[line][column - 1]);
  2002. if (only_whitespace && !is_whitespace) {
  2003. break;
  2004. } else if (only_char && !is_text_char) {
  2005. break;
  2006. } else if (only_symbols && (is_whitespace || is_text_char)) {
  2007. break;
  2008. }
  2009. column--;
  2010. }
  2011. _remove_text(line, column, cursor.line, cursor.column);
  2012. cursor_set_line(line);
  2013. cursor_set_column(column);
  2014. } else {
  2015. if (cursor.line > 0 && is_line_hidden(cursor.line - 1))
  2016. unfold_line(cursor.line - 1);
  2017. backspace_at_cursor();
  2018. }
  2019. } break;
  2020. case KEY_KP_4: {
  2021. if (k->get_unicode() != 0) {
  2022. scancode_handled = false;
  2023. break;
  2024. }
  2025. // numlock disabled. fallthrough to key_left
  2026. }
  2027. case KEY_LEFT: {
  2028. if (k->get_shift())
  2029. _pre_shift_selection();
  2030. #ifdef APPLE_STYLE_KEYS
  2031. else
  2032. #else
  2033. else if (!k->get_alt())
  2034. #endif
  2035. deselect();
  2036. #ifdef APPLE_STYLE_KEYS
  2037. if (k->get_command()) {
  2038. cursor_set_column(0);
  2039. } else if (k->get_alt()) {
  2040. #else
  2041. if (k->get_alt()) {
  2042. scancode_handled = false;
  2043. break;
  2044. } else if (k->get_command()) {
  2045. #endif
  2046. bool prev_char = false;
  2047. int cc = cursor.column;
  2048. if (cc == 0 && cursor.line > 0) {
  2049. cursor_set_line(cursor.line - 1);
  2050. cursor_set_column(text[cursor.line].length());
  2051. break;
  2052. }
  2053. while (cc > 0) {
  2054. bool ischar = _is_text_char(text[cursor.line][cc - 1]);
  2055. if (prev_char && !ischar)
  2056. break;
  2057. prev_char = ischar;
  2058. cc--;
  2059. }
  2060. cursor_set_column(cc);
  2061. } else if (cursor.column == 0) {
  2062. if (cursor.line > 0) {
  2063. cursor_set_line(cursor.line - num_lines_from(CLAMP(cursor.line - 1, 0, text.size() - 1), -1));
  2064. cursor_set_column(text[cursor.line].length());
  2065. }
  2066. } else {
  2067. cursor_set_column(cursor_get_column() - 1);
  2068. }
  2069. if (k->get_shift())
  2070. _post_shift_selection();
  2071. } break;
  2072. case KEY_KP_6: {
  2073. if (k->get_unicode() != 0) {
  2074. scancode_handled = false;
  2075. break;
  2076. }
  2077. // numlock disabled. fallthrough to key_right
  2078. }
  2079. case KEY_RIGHT: {
  2080. if (k->get_shift())
  2081. _pre_shift_selection();
  2082. #ifdef APPLE_STYLE_KEYS
  2083. else
  2084. #else
  2085. else if (!k->get_alt())
  2086. #endif
  2087. deselect();
  2088. #ifdef APPLE_STYLE_KEYS
  2089. if (k->get_command()) {
  2090. cursor_set_column(text[cursor.line].length());
  2091. } else if (k->get_alt()) {
  2092. #else
  2093. if (k->get_alt()) {
  2094. scancode_handled = false;
  2095. break;
  2096. } else if (k->get_command()) {
  2097. #endif
  2098. bool prev_char = false;
  2099. int cc = cursor.column;
  2100. if (cc == text[cursor.line].length() && cursor.line < text.size() - 1) {
  2101. cursor_set_line(cursor.line + 1);
  2102. cursor_set_column(0);
  2103. break;
  2104. }
  2105. while (cc < text[cursor.line].length()) {
  2106. bool ischar = _is_text_char(text[cursor.line][cc]);
  2107. if (prev_char && !ischar)
  2108. break;
  2109. prev_char = ischar;
  2110. cc++;
  2111. }
  2112. cursor_set_column(cc);
  2113. } else if (cursor.column == text[cursor.line].length()) {
  2114. if (cursor.line < text.size() - 1) {
  2115. cursor_set_line(cursor_get_line() + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1), true, false);
  2116. cursor_set_column(0);
  2117. }
  2118. } else {
  2119. cursor_set_column(cursor_get_column() + 1);
  2120. }
  2121. if (k->get_shift())
  2122. _post_shift_selection();
  2123. } break;
  2124. case KEY_KP_8: {
  2125. if (k->get_unicode() != 0) {
  2126. scancode_handled = false;
  2127. break;
  2128. }
  2129. // numlock disabled. fallthrough to key_up
  2130. }
  2131. case KEY_UP: {
  2132. if (k->get_shift())
  2133. _pre_shift_selection();
  2134. if (k->get_alt()) {
  2135. scancode_handled = false;
  2136. break;
  2137. }
  2138. #ifndef APPLE_STYLE_KEYS
  2139. if (k->get_command()) {
  2140. _scroll_lines_up();
  2141. break;
  2142. }
  2143. #else
  2144. if (k->get_command() && k->get_alt()) {
  2145. _scroll_lines_up();
  2146. break;
  2147. }
  2148. if (k->get_command())
  2149. cursor_set_line(0);
  2150. else
  2151. #endif
  2152. cursor_set_line(cursor_get_line() - num_lines_from(CLAMP(cursor.line - 1, 0, text.size() - 1), -1));
  2153. if (k->get_shift())
  2154. _post_shift_selection();
  2155. _cancel_code_hint();
  2156. } break;
  2157. case KEY_KP_2: {
  2158. if (k->get_unicode() != 0) {
  2159. scancode_handled = false;
  2160. break;
  2161. }
  2162. // numlock disabled. fallthrough to key_down
  2163. }
  2164. case KEY_DOWN: {
  2165. if (k->get_shift())
  2166. _pre_shift_selection();
  2167. if (k->get_alt()) {
  2168. scancode_handled = false;
  2169. break;
  2170. }
  2171. #ifndef APPLE_STYLE_KEYS
  2172. if (k->get_command()) {
  2173. _scroll_lines_down();
  2174. break;
  2175. }
  2176. #else
  2177. if (k->get_command() && k->get_alt()) {
  2178. _scroll_lines_down();
  2179. break;
  2180. }
  2181. if (k->get_command())
  2182. cursor_set_line(text.size() - 1, true, false);
  2183. else
  2184. #endif
  2185. cursor_set_line(cursor_get_line() + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1), true, false);
  2186. if (k->get_shift())
  2187. _post_shift_selection();
  2188. _cancel_code_hint();
  2189. } break;
  2190. case KEY_DELETE: {
  2191. if (readonly)
  2192. break;
  2193. if (k->get_shift() && !k->get_command() && !k->get_alt()) {
  2194. cut();
  2195. break;
  2196. }
  2197. int curline_len = text[cursor.line].length();
  2198. if (cursor.line == text.size() - 1 && cursor.column == curline_len)
  2199. break; //nothing to do
  2200. int next_line = cursor.column < curline_len ? cursor.line : cursor.line + 1;
  2201. int next_column;
  2202. #ifdef APPLE_STYLE_KEYS
  2203. if (k->get_alt() && cursor.column < curline_len - 1) {
  2204. #else
  2205. if (k->get_alt()) {
  2206. scancode_handled = false;
  2207. break;
  2208. } else if (k->get_command() && cursor.column < curline_len - 1) {
  2209. #endif
  2210. int line = cursor.line;
  2211. int column = cursor.column;
  2212. // check if we are removing a single whitespace, if so remove it and the next char type
  2213. // else we just remove the whitespace
  2214. bool only_whitespace = false;
  2215. if (_is_whitespace(text[line][column]) && _is_whitespace(text[line][column + 1])) {
  2216. only_whitespace = true;
  2217. } else if (_is_whitespace(text[line][column])) {
  2218. // remove the single whitespace
  2219. column++;
  2220. }
  2221. // check if its a text char
  2222. bool only_char = (_is_text_char(text[line][column]) && !only_whitespace);
  2223. // if its not whitespace or char then symbol.
  2224. bool only_symbols = !(only_whitespace || only_char);
  2225. while (column < curline_len) {
  2226. bool is_whitespace = _is_whitespace(text[line][column]);
  2227. bool is_text_char = _is_text_char(text[line][column]);
  2228. if (only_whitespace && !is_whitespace) {
  2229. break;
  2230. } else if (only_char && !is_text_char) {
  2231. break;
  2232. } else if (only_symbols && (is_whitespace || is_text_char)) {
  2233. break;
  2234. }
  2235. column++;
  2236. }
  2237. next_line = line;
  2238. next_column = column;
  2239. } else {
  2240. next_column = cursor.column < curline_len ? (cursor.column + 1) : 0;
  2241. }
  2242. _remove_text(cursor.line, cursor.column, next_line, next_column);
  2243. update();
  2244. } break;
  2245. case KEY_KP_7: {
  2246. if (k->get_unicode() != 0) {
  2247. scancode_handled = false;
  2248. break;
  2249. }
  2250. // numlock disabled. fallthrough to key_home
  2251. }
  2252. #ifdef APPLE_STYLE_KEYS
  2253. case KEY_HOME: {
  2254. if (k->get_shift())
  2255. _pre_shift_selection();
  2256. cursor_set_line(0);
  2257. if (k->get_shift())
  2258. _post_shift_selection();
  2259. else if (k->get_command() || k->get_control())
  2260. deselect();
  2261. } break;
  2262. #else
  2263. case KEY_HOME: {
  2264. if (k->get_shift())
  2265. _pre_shift_selection();
  2266. if (k->get_command()) {
  2267. cursor_set_line(0);
  2268. cursor_set_column(0);
  2269. } else {
  2270. // compute whitespace symbols seq length
  2271. int current_line_whitespace_len = 0;
  2272. while (current_line_whitespace_len < text[cursor.line].length()) {
  2273. CharType c = text[cursor.line][current_line_whitespace_len];
  2274. if (c != '\t' && c != ' ')
  2275. break;
  2276. current_line_whitespace_len++;
  2277. }
  2278. if (cursor_get_column() == current_line_whitespace_len)
  2279. cursor_set_column(0);
  2280. else
  2281. cursor_set_column(current_line_whitespace_len);
  2282. }
  2283. if (k->get_shift())
  2284. _post_shift_selection();
  2285. else if (k->get_command() || k->get_control())
  2286. deselect();
  2287. _cancel_completion();
  2288. completion_hint = "";
  2289. } break;
  2290. #endif
  2291. case KEY_KP_1: {
  2292. if (k->get_unicode() != 0) {
  2293. scancode_handled = false;
  2294. break;
  2295. }
  2296. // numlock disabled. fallthrough to key_end
  2297. }
  2298. #ifdef APPLE_STYLE_KEYS
  2299. case KEY_END: {
  2300. if (k->get_shift())
  2301. _pre_shift_selection();
  2302. cursor_set_line(text.size() - 1, true, false);
  2303. if (k->get_shift())
  2304. _post_shift_selection();
  2305. else if (k->get_command() || k->get_control())
  2306. deselect();
  2307. } break;
  2308. #else
  2309. case KEY_END: {
  2310. if (k->get_shift())
  2311. _pre_shift_selection();
  2312. if (k->get_command())
  2313. cursor_set_line(text.size() - 1, true, false);
  2314. cursor_set_column(text[cursor.line].length());
  2315. if (k->get_shift())
  2316. _post_shift_selection();
  2317. else if (k->get_command() || k->get_control())
  2318. deselect();
  2319. _cancel_completion();
  2320. completion_hint = "";
  2321. } break;
  2322. #endif
  2323. case KEY_KP_9: {
  2324. if (k->get_unicode() != 0) {
  2325. scancode_handled = false;
  2326. break;
  2327. }
  2328. // numlock disabled. fallthrough to key_pageup
  2329. }
  2330. case KEY_PAGEUP: {
  2331. if (k->get_shift())
  2332. _pre_shift_selection();
  2333. cursor_set_line(cursor_get_line() - num_lines_from(cursor.line, -get_visible_rows()), true, false);
  2334. if (k->get_shift())
  2335. _post_shift_selection();
  2336. _cancel_completion();
  2337. completion_hint = "";
  2338. } break;
  2339. case KEY_KP_3: {
  2340. if (k->get_unicode() != 0) {
  2341. scancode_handled = false;
  2342. break;
  2343. }
  2344. // numlock disabled. fallthrough to key_pageup
  2345. }
  2346. case KEY_PAGEDOWN: {
  2347. if (k->get_shift())
  2348. _pre_shift_selection();
  2349. cursor_set_line(cursor_get_line() + num_lines_from(cursor.line, get_visible_rows()), true, false);
  2350. if (k->get_shift())
  2351. _post_shift_selection();
  2352. _cancel_completion();
  2353. completion_hint = "";
  2354. } break;
  2355. case KEY_A: {
  2356. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2357. scancode_handled = false;
  2358. break;
  2359. }
  2360. select_all();
  2361. } break;
  2362. case KEY_X: {
  2363. if (readonly) {
  2364. break;
  2365. }
  2366. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2367. scancode_handled = false;
  2368. break;
  2369. }
  2370. cut();
  2371. } break;
  2372. case KEY_C: {
  2373. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2374. scancode_handled = false;
  2375. break;
  2376. }
  2377. copy();
  2378. } break;
  2379. case KEY_Z: {
  2380. if (!k->get_command()) {
  2381. scancode_handled = false;
  2382. break;
  2383. }
  2384. if (k->get_shift())
  2385. redo();
  2386. else
  2387. undo();
  2388. } break;
  2389. case KEY_Y: {
  2390. if (!k->get_command()) {
  2391. scancode_handled = false;
  2392. break;
  2393. }
  2394. redo();
  2395. } break;
  2396. case KEY_V: {
  2397. if (readonly) {
  2398. break;
  2399. }
  2400. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2401. scancode_handled = false;
  2402. break;
  2403. }
  2404. paste();
  2405. } break;
  2406. case KEY_SPACE: {
  2407. #ifdef OSX_ENABLED
  2408. if (completion_enabled && k->get_metakey()) { //cmd-space is spotlight shortcut in OSX
  2409. #else
  2410. if (completion_enabled && k->get_command()) {
  2411. #endif
  2412. query_code_comple();
  2413. scancode_handled = true;
  2414. } else {
  2415. scancode_handled = false;
  2416. }
  2417. } break;
  2418. case KEY_U: {
  2419. if (!k->get_command() || k->get_shift()) {
  2420. scancode_handled = false;
  2421. break;
  2422. } else {
  2423. if (selection.active) {
  2424. int ini = selection.from_line;
  2425. int end = selection.to_line;
  2426. for (int i = ini; i <= end; i++) {
  2427. if (get_line(i).begins_with("#"))
  2428. _remove_text(i, 0, i, 1);
  2429. }
  2430. } else {
  2431. if (get_line(cursor.line).begins_with("#")) {
  2432. _remove_text(cursor.line, 0, cursor.line, 1);
  2433. if (cursor.column >= get_line(cursor.line).length()) {
  2434. cursor.column = MAX(0, get_line(cursor.line).length() - 1);
  2435. }
  2436. }
  2437. }
  2438. update();
  2439. }
  2440. } break;
  2441. default: {
  2442. scancode_handled = false;
  2443. } break;
  2444. }
  2445. if (scancode_handled)
  2446. accept_event();
  2447. /*
  2448. if (!scancode_handled && !k->get_command() && !k->get_alt()) {
  2449. if (k->get_unicode()>=32) {
  2450. if (readonly)
  2451. break;
  2452. accept_event();
  2453. } else {
  2454. break;
  2455. }
  2456. }
  2457. */
  2458. if (k->get_scancode() == KEY_INSERT) {
  2459. set_insert_mode(!insert_mode);
  2460. accept_event();
  2461. return;
  2462. }
  2463. if (!scancode_handled && !k->get_command()) { //for german kbds
  2464. if (k->get_unicode() >= 32) {
  2465. if (readonly)
  2466. return;
  2467. // remove the old character if in insert mode and no selection
  2468. if (insert_mode && !had_selection) {
  2469. begin_complex_operation();
  2470. // make sure we don't try and remove empty space
  2471. if (cursor.column < get_line(cursor.line).length()) {
  2472. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  2473. }
  2474. }
  2475. const CharType chr[2] = { (CharType)k->get_unicode(), 0 };
  2476. if (completion_hint != "" && k->get_unicode() == ')') {
  2477. completion_hint = "";
  2478. }
  2479. if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  2480. _consume_pair_symbol(chr[0]);
  2481. } else {
  2482. _insert_text_at_cursor(chr);
  2483. }
  2484. if (insert_mode && !had_selection) {
  2485. end_complex_operation();
  2486. }
  2487. if (selection.active != had_selection) {
  2488. end_complex_operation();
  2489. }
  2490. accept_event();
  2491. } else {
  2492. }
  2493. }
  2494. return;
  2495. }
  2496. }
  2497. void TextEdit::_scroll_up(real_t p_delta) {
  2498. if (scrolling) {
  2499. target_v_scroll = (target_v_scroll - p_delta);
  2500. } else {
  2501. target_v_scroll = (v_scroll->get_value() - p_delta);
  2502. }
  2503. if (smooth_scroll_enabled) {
  2504. if (target_v_scroll <= 0) {
  2505. target_v_scroll = 0;
  2506. }
  2507. scrolling = true;
  2508. set_physics_process(true);
  2509. } else {
  2510. v_scroll->set_value(target_v_scroll);
  2511. }
  2512. }
  2513. void TextEdit::_scroll_down(real_t p_delta) {
  2514. if (scrolling) {
  2515. target_v_scroll = (target_v_scroll + p_delta);
  2516. } else {
  2517. target_v_scroll = (v_scroll->get_value() + p_delta);
  2518. }
  2519. if (smooth_scroll_enabled) {
  2520. int max_v_scroll = get_total_unhidden_rows();
  2521. if (!scroll_past_end_of_file_enabled) {
  2522. max_v_scroll -= get_visible_rows();
  2523. max_v_scroll = CLAMP(max_v_scroll, 0, get_total_unhidden_rows());
  2524. }
  2525. if (target_v_scroll > max_v_scroll) {
  2526. target_v_scroll = max_v_scroll;
  2527. }
  2528. scrolling = true;
  2529. set_physics_process(true);
  2530. } else {
  2531. v_scroll->set_value(target_v_scroll);
  2532. }
  2533. }
  2534. void TextEdit::_pre_shift_selection() {
  2535. if (!selection.active || selection.selecting_mode == Selection::MODE_NONE) {
  2536. selection.selecting_line = cursor.line;
  2537. selection.selecting_column = cursor.column;
  2538. selection.active = true;
  2539. }
  2540. selection.selecting_mode = Selection::MODE_SHIFT;
  2541. }
  2542. void TextEdit::_post_shift_selection() {
  2543. if (selection.active && selection.selecting_mode == Selection::MODE_SHIFT) {
  2544. select(selection.selecting_line, selection.selecting_column, cursor.line, cursor.column);
  2545. update();
  2546. }
  2547. selection.selecting_text = true;
  2548. }
  2549. void TextEdit::_scroll_lines_up() {
  2550. scrolling = false;
  2551. // adjust the vertical scroll
  2552. if (get_v_scroll() >= 0) {
  2553. set_v_scroll(get_v_scroll() - 1);
  2554. }
  2555. // adjust the cursor
  2556. int num_lines = num_lines_from(CLAMP(cursor.line_ofs, 0, text.size() - 1), get_visible_rows());
  2557. if (cursor.line >= cursor.line_ofs + num_lines && !selection.active) {
  2558. cursor_set_line(cursor.line_ofs + num_lines, false, false);
  2559. }
  2560. }
  2561. void TextEdit::_scroll_lines_down() {
  2562. scrolling = false;
  2563. // calculate the maximum vertical scroll position
  2564. int max_v_scroll = get_total_unhidden_rows();
  2565. if (!scroll_past_end_of_file_enabled) {
  2566. max_v_scroll -= get_visible_rows();
  2567. max_v_scroll = CLAMP(max_v_scroll, 0, get_total_unhidden_rows());
  2568. }
  2569. // adjust the vertical scroll
  2570. if (get_v_scroll() < max_v_scroll) {
  2571. set_v_scroll(get_v_scroll() + 1);
  2572. }
  2573. // adjust the cursor
  2574. if (cursor.line <= cursor.line_ofs - 1 && !selection.active) {
  2575. cursor_set_line(cursor.line_ofs, false, false);
  2576. }
  2577. }
  2578. /**** TEXT EDIT CORE API ****/
  2579. void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
  2580. //save for undo...
  2581. ERR_FAIL_INDEX(p_line, text.size());
  2582. ERR_FAIL_COND(p_char < 0);
  2583. /* STEP 1 add spaces if the char is greater than the end of the line */
  2584. while (p_char > text[p_line].length()) {
  2585. text.set(p_line, text[p_line] + String::chr(' '));
  2586. }
  2587. /* STEP 2 separate dest string in pre and post text */
  2588. String preinsert_text = text[p_line].substr(0, p_char);
  2589. String postinsert_text = text[p_line].substr(p_char, text[p_line].size());
  2590. /* STEP 3 remove \r from source text and separate in substrings */
  2591. //buh bye \r and split
  2592. Vector<String> substrings = p_text.replace("\r", "").split("\n");
  2593. for (int i = 0; i < substrings.size(); i++) {
  2594. //insert the substrings
  2595. if (i == 0) {
  2596. text.set(p_line, preinsert_text + substrings[i]);
  2597. } else {
  2598. text.insert(p_line + i, substrings[i]);
  2599. }
  2600. if (i == substrings.size() - 1) {
  2601. text.set(p_line + i, text[p_line + i] + postinsert_text);
  2602. }
  2603. }
  2604. // if we are just making a new empty line, reset breakpoints and hidden status
  2605. if (p_char == 0 && p_text.replace("\r", "") == "\n") {
  2606. text.set_breakpoint(p_line + 1, text.is_breakpoint(p_line));
  2607. text.set_hidden(p_line + 1, text.is_hidden(p_line));
  2608. text.set_breakpoint(p_line, false);
  2609. text.set_hidden(p_line, false);
  2610. }
  2611. r_end_line = p_line + substrings.size() - 1;
  2612. r_end_column = text[r_end_line].length() - postinsert_text.length();
  2613. if (!text_changed_dirty && !setting_text) {
  2614. if (is_inside_tree())
  2615. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  2616. text_changed_dirty = true;
  2617. }
  2618. }
  2619. String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
  2620. ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
  2621. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
  2622. ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
  2623. ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
  2624. ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // from > to
  2625. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // from > to
  2626. String ret;
  2627. for (int i = p_from_line; i <= p_to_line; i++) {
  2628. int begin = (i == p_from_line) ? p_from_column : 0;
  2629. int end = (i == p_to_line) ? p_to_column : text[i].length();
  2630. if (i > p_from_line)
  2631. ret += "\n";
  2632. ret += text[i].substr(begin, end - begin);
  2633. }
  2634. return ret;
  2635. }
  2636. void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  2637. ERR_FAIL_INDEX(p_from_line, text.size());
  2638. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  2639. ERR_FAIL_INDEX(p_to_line, text.size());
  2640. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  2641. ERR_FAIL_COND(p_to_line < p_from_line); // from > to
  2642. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // from > to
  2643. String pre_text = text[p_from_line].substr(0, p_from_column);
  2644. String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length());
  2645. for (int i = p_from_line; i < p_to_line; i++) {
  2646. text.remove(p_from_line + 1);
  2647. }
  2648. text.set(p_from_line, pre_text + post_text);
  2649. if (!text_changed_dirty && !setting_text) {
  2650. if (is_inside_tree())
  2651. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  2652. text_changed_dirty = true;
  2653. }
  2654. }
  2655. void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) {
  2656. if (!setting_text)
  2657. idle_detect->start();
  2658. if (undo_enabled) {
  2659. _clear_redo();
  2660. }
  2661. int retline, retchar;
  2662. _base_insert_text(p_line, p_char, p_text, retline, retchar);
  2663. if (r_end_line)
  2664. *r_end_line = retline;
  2665. if (r_end_char)
  2666. *r_end_char = retchar;
  2667. if (!undo_enabled)
  2668. return;
  2669. /* UNDO!! */
  2670. TextOperation op;
  2671. op.type = TextOperation::TYPE_INSERT;
  2672. op.from_line = p_line;
  2673. op.from_column = p_char;
  2674. op.to_line = retline;
  2675. op.to_column = retchar;
  2676. op.text = p_text;
  2677. op.version = ++version;
  2678. op.chain_forward = false;
  2679. op.chain_backward = false;
  2680. //see if it shold just be set as current op
  2681. if (current_op.type != op.type) {
  2682. op.prev_version = get_version();
  2683. _push_current_op();
  2684. current_op = op;
  2685. return; //set as current op, return
  2686. }
  2687. //see if it can be merged
  2688. if (current_op.to_line != p_line || current_op.to_column != p_char) {
  2689. op.prev_version = get_version();
  2690. _push_current_op();
  2691. current_op = op;
  2692. return; //set as current op, return
  2693. }
  2694. //merge current op
  2695. current_op.text += p_text;
  2696. current_op.to_column = retchar;
  2697. current_op.to_line = retline;
  2698. current_op.version = op.version;
  2699. }
  2700. void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  2701. if (!setting_text)
  2702. idle_detect->start();
  2703. String text;
  2704. if (undo_enabled) {
  2705. _clear_redo();
  2706. text = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
  2707. }
  2708. _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  2709. if (!undo_enabled)
  2710. return;
  2711. /* UNDO!! */
  2712. TextOperation op;
  2713. op.type = TextOperation::TYPE_REMOVE;
  2714. op.from_line = p_from_line;
  2715. op.from_column = p_from_column;
  2716. op.to_line = p_to_line;
  2717. op.to_column = p_to_column;
  2718. op.text = text;
  2719. op.version = ++version;
  2720. op.chain_forward = false;
  2721. op.chain_backward = false;
  2722. //see if it shold just be set as current op
  2723. if (current_op.type != op.type) {
  2724. op.prev_version = get_version();
  2725. _push_current_op();
  2726. current_op = op;
  2727. return; //set as current op, return
  2728. }
  2729. //see if it can be merged
  2730. if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) {
  2731. //basckace or similar
  2732. current_op.text = text + current_op.text;
  2733. current_op.from_line = p_from_line;
  2734. current_op.from_column = p_from_column;
  2735. return; //update current op
  2736. }
  2737. if (current_op.from_line == p_from_line && current_op.from_column == p_from_column) {
  2738. //current_op.text=text+current_op.text;
  2739. //current_op.from_line=p_from_line;
  2740. //current_op.from_column=p_from_column;
  2741. //return; //update current op
  2742. }
  2743. op.prev_version = get_version();
  2744. _push_current_op();
  2745. current_op = op;
  2746. }
  2747. void TextEdit::_insert_text_at_cursor(const String &p_text) {
  2748. int new_column, new_line;
  2749. _insert_text(cursor.line, cursor.column, p_text, &new_line, &new_column);
  2750. cursor_set_line(new_line);
  2751. cursor_set_column(new_column);
  2752. update();
  2753. }
  2754. int TextEdit::get_char_count() {
  2755. int totalsize = 0;
  2756. for (int i = 0; i < text.size(); i++) {
  2757. if (i > 0)
  2758. totalsize++; // incliude \n
  2759. totalsize += text[i].length();
  2760. }
  2761. return totalsize; // omit last \n
  2762. }
  2763. Size2 TextEdit::get_minimum_size() const {
  2764. return cache.style_normal->get_minimum_size();
  2765. }
  2766. int TextEdit::get_visible_rows() const {
  2767. int total = cache.size.height;
  2768. total -= cache.style_normal->get_minimum_size().height;
  2769. total /= get_row_height();
  2770. return total;
  2771. }
  2772. int TextEdit::get_total_unhidden_rows() const {
  2773. if (!is_hiding_enabled())
  2774. return text.size();
  2775. int total_unhidden = 0;
  2776. for (int i = 0; i < text.size(); i++) {
  2777. if (!text.is_hidden(i))
  2778. total_unhidden++;
  2779. }
  2780. return total_unhidden;
  2781. }
  2782. double TextEdit::get_line_scroll_pos(bool p_recalculate) const {
  2783. if (!is_hiding_enabled())
  2784. return cursor.line_ofs;
  2785. if (!p_recalculate)
  2786. return line_scroll_pos;
  2787. // count num unhidden lines to the cursor line ofs
  2788. double new_line_scroll_pos = 0;
  2789. int to = CLAMP(cursor.line_ofs, 0, text.size() - 1);
  2790. for (int i = 0; i < to; i++) {
  2791. if (!text.is_hidden(i))
  2792. new_line_scroll_pos++;
  2793. }
  2794. return new_line_scroll_pos;
  2795. }
  2796. void TextEdit::update_line_scroll_pos() {
  2797. if (!is_hiding_enabled()) {
  2798. line_scroll_pos = cursor.line_ofs;
  2799. return;
  2800. }
  2801. // count num unhidden lines to the cursor line ofs
  2802. double new_line_scroll_pos = 0;
  2803. int to = CLAMP(cursor.line_ofs, 0, text.size() - 1);
  2804. for (int i = 0; i < to; i++) {
  2805. if (!text.is_hidden(i))
  2806. new_line_scroll_pos++;
  2807. }
  2808. line_scroll_pos = new_line_scroll_pos;
  2809. }
  2810. void TextEdit::adjust_viewport_to_cursor() {
  2811. scrolling = false;
  2812. if (cursor.line_ofs > cursor.line) {
  2813. cursor.line_ofs = cursor.line;
  2814. }
  2815. int visible_width = cache.size.width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width;
  2816. if (v_scroll->is_visible_in_tree())
  2817. visible_width -= v_scroll->get_combined_minimum_size().width;
  2818. visible_width -= 20; // give it a little more space
  2819. int visible_rows = get_visible_rows();
  2820. if (h_scroll->is_visible_in_tree() && !scroll_past_end_of_file_enabled)
  2821. visible_rows -= ((h_scroll->get_combined_minimum_size().height - 1) / get_row_height());
  2822. int num_rows = num_lines_from(CLAMP(cursor.line_ofs, 0, text.size() - 1), MIN(visible_rows, text.size() - 1 - cursor.line_ofs));
  2823. // make sure the cursor is on the screen
  2824. // above the caret
  2825. if (cursor.line > (cursor.line_ofs + MAX(num_rows, visible_rows))) {
  2826. cursor.line_ofs = cursor.line - num_lines_from(cursor.line, -visible_rows) + 1;
  2827. }
  2828. // below the caret
  2829. if (cursor.line_ofs == cursor.line) {
  2830. cursor.line_ofs = cursor.line - 2;
  2831. }
  2832. int line_ofs_max = text.size() - 1;
  2833. if (!scroll_past_end_of_file_enabled) {
  2834. line_ofs_max -= num_lines_from(text.size() - 1, -visible_rows) - 1;
  2835. line_ofs_max += (h_scroll->is_visible_in_tree() ? 1 : 0);
  2836. line_ofs_max += (cursor.line == text.size() - 1 ? 1 : 0);
  2837. }
  2838. line_ofs_max = MAX(line_ofs_max, 0);
  2839. cursor.line_ofs = CLAMP(cursor.line_ofs, 0, line_ofs_max);
  2840. // adjust x offset
  2841. int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]);
  2842. if (cursor_x > (cursor.x_ofs + visible_width))
  2843. cursor.x_ofs = cursor_x - visible_width + 1;
  2844. if (cursor_x < cursor.x_ofs)
  2845. cursor.x_ofs = cursor_x;
  2846. updating_scrolls = true;
  2847. h_scroll->set_value(cursor.x_ofs);
  2848. update_line_scroll_pos();
  2849. double new_v_scroll = get_line_scroll_pos();
  2850. // keep offset if smooth scroll is enabled
  2851. if (smooth_scroll_enabled) {
  2852. new_v_scroll += fmod(v_scroll->get_value(), 1.0);
  2853. }
  2854. v_scroll->set_value(new_v_scroll);
  2855. updating_scrolls = false;
  2856. update();
  2857. }
  2858. void TextEdit::center_viewport_to_cursor() {
  2859. scrolling = false;
  2860. if (cursor.line_ofs > cursor.line)
  2861. cursor.line_ofs = cursor.line;
  2862. if (is_line_hidden(cursor.line))
  2863. unfold_line(cursor.line);
  2864. int visible_width = cache.size.width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width;
  2865. if (v_scroll->is_visible_in_tree())
  2866. visible_width -= v_scroll->get_combined_minimum_size().width;
  2867. visible_width -= 20; // give it a little more space
  2868. int visible_rows = get_visible_rows();
  2869. if (h_scroll->is_visible_in_tree())
  2870. visible_rows -= ((h_scroll->get_combined_minimum_size().height - 1) / get_row_height());
  2871. int max_ofs = text.size() - (scroll_past_end_of_file_enabled ? 1 : num_lines_from(text.size() - 1, -visible_rows));
  2872. cursor.line_ofs = CLAMP(cursor.line - num_lines_from(cursor.line - visible_rows / 2, -visible_rows / 2), 0, max_ofs);
  2873. int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]);
  2874. if (cursor_x > (cursor.x_ofs + visible_width))
  2875. cursor.x_ofs = cursor_x - visible_width + 1;
  2876. if (cursor_x < cursor.x_ofs)
  2877. cursor.x_ofs = cursor_x;
  2878. updating_scrolls = true;
  2879. h_scroll->set_value(cursor.x_ofs);
  2880. update_line_scroll_pos();
  2881. double new_v_scroll = get_line_scroll_pos();
  2882. // keep offset if smooth scroll is enabled
  2883. if (smooth_scroll_enabled) {
  2884. new_v_scroll += fmod(v_scroll->get_value(), 1.0);
  2885. }
  2886. v_scroll->set_value(new_v_scroll);
  2887. updating_scrolls = false;
  2888. update();
  2889. }
  2890. void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
  2891. if (p_col < 0)
  2892. p_col = 0;
  2893. cursor.column = p_col;
  2894. if (cursor.column > get_line(cursor.line).length())
  2895. cursor.column = get_line(cursor.line).length();
  2896. cursor.last_fit_x = get_column_x_offset(cursor.column, get_line(cursor.line));
  2897. if (p_adjust_viewport)
  2898. adjust_viewport_to_cursor();
  2899. if (!cursor_changed_dirty) {
  2900. if (is_inside_tree())
  2901. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  2902. cursor_changed_dirty = true;
  2903. }
  2904. }
  2905. void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport, bool p_can_be_hidden) {
  2906. if (setting_row)
  2907. return;
  2908. setting_row = true;
  2909. if (p_row < 0)
  2910. p_row = 0;
  2911. if (p_row >= (int)text.size())
  2912. p_row = (int)text.size() - 1;
  2913. if (!p_can_be_hidden) {
  2914. if (is_line_hidden(CLAMP(p_row, 0, text.size() - 1))) {
  2915. int move_down = num_lines_from(p_row, 1) - 1;
  2916. if (p_row + move_down <= text.size() - 1 && !is_line_hidden(p_row + move_down)) {
  2917. p_row += move_down;
  2918. } else {
  2919. int move_up = num_lines_from(p_row, -1) - 1;
  2920. if (p_row - move_up > 0 && !is_line_hidden(p_row - move_up)) {
  2921. p_row -= move_up;
  2922. } else {
  2923. WARN_PRINTS(("Cursor set to hidden line " + itos(p_row) + " and there are no nonhidden lines."));
  2924. }
  2925. }
  2926. }
  2927. }
  2928. cursor.line = p_row;
  2929. cursor.column = get_char_pos_for(cursor.last_fit_x, get_line(cursor.line));
  2930. if (p_adjust_viewport)
  2931. adjust_viewport_to_cursor();
  2932. setting_row = false;
  2933. if (!cursor_changed_dirty) {
  2934. if (is_inside_tree())
  2935. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  2936. cursor_changed_dirty = true;
  2937. }
  2938. }
  2939. int TextEdit::cursor_get_column() const {
  2940. return cursor.column;
  2941. }
  2942. int TextEdit::cursor_get_line() const {
  2943. return cursor.line;
  2944. }
  2945. bool TextEdit::cursor_get_blink_enabled() const {
  2946. return caret_blink_enabled;
  2947. }
  2948. void TextEdit::cursor_set_blink_enabled(const bool p_enabled) {
  2949. caret_blink_enabled = p_enabled;
  2950. if (p_enabled) {
  2951. caret_blink_timer->start();
  2952. } else {
  2953. caret_blink_timer->stop();
  2954. }
  2955. draw_caret = true;
  2956. }
  2957. float TextEdit::cursor_get_blink_speed() const {
  2958. return caret_blink_timer->get_wait_time();
  2959. }
  2960. void TextEdit::cursor_set_blink_speed(const float p_speed) {
  2961. ERR_FAIL_COND(p_speed <= 0);
  2962. caret_blink_timer->set_wait_time(p_speed);
  2963. }
  2964. void TextEdit::cursor_set_block_mode(const bool p_enable) {
  2965. block_caret = p_enable;
  2966. update();
  2967. }
  2968. bool TextEdit::cursor_is_block_mode() const {
  2969. return block_caret;
  2970. }
  2971. void TextEdit::_v_scroll_input() {
  2972. scrolling = false;
  2973. }
  2974. void TextEdit::_scroll_moved(double p_to_val) {
  2975. if (updating_scrolls)
  2976. return;
  2977. if (h_scroll->is_visible_in_tree())
  2978. cursor.x_ofs = h_scroll->get_value();
  2979. if (v_scroll->is_visible_in_tree()) {
  2980. double val = v_scroll->get_value();
  2981. cursor.line_ofs = num_lines_from(0, (int)floor(val));
  2982. line_scroll_pos = (int)floor(val);
  2983. }
  2984. update();
  2985. }
  2986. int TextEdit::get_row_height() const {
  2987. return cache.font->get_height() + cache.line_spacing;
  2988. }
  2989. int TextEdit::get_char_pos_for(int p_px, String p_str) const {
  2990. int px = 0;
  2991. int c = 0;
  2992. int tab_w = cache.font->get_char_size(' ').width * indent_size;
  2993. while (c < p_str.length()) {
  2994. int w = 0;
  2995. if (p_str[c] == '\t') {
  2996. int left = px % tab_w;
  2997. if (left == 0)
  2998. w = tab_w;
  2999. else
  3000. w = tab_w - px % tab_w; // is right...
  3001. } else {
  3002. w = cache.font->get_char_size(p_str[c], p_str[c + 1]).width;
  3003. }
  3004. if (p_px < (px + w / 2))
  3005. break;
  3006. px += w;
  3007. c++;
  3008. }
  3009. return c;
  3010. }
  3011. int TextEdit::get_column_x_offset(int p_char, String p_str) {
  3012. int px = 0;
  3013. int tab_w = cache.font->get_char_size(' ').width * indent_size;
  3014. for (int i = 0; i < p_char; i++) {
  3015. if (i >= p_str.length())
  3016. break;
  3017. if (p_str[i] == '\t') {
  3018. int left = px % tab_w;
  3019. if (left == 0)
  3020. px += tab_w;
  3021. else
  3022. px += tab_w - px % tab_w; // is right...
  3023. } else {
  3024. px += cache.font->get_char_size(p_str[i], p_str[i + 1]).width;
  3025. }
  3026. }
  3027. return px;
  3028. }
  3029. void TextEdit::insert_text_at_cursor(const String &p_text) {
  3030. if (selection.active) {
  3031. cursor_set_line(selection.from_line);
  3032. cursor_set_column(selection.from_column);
  3033. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3034. selection.active = false;
  3035. selection.selecting_mode = Selection::MODE_NONE;
  3036. }
  3037. _insert_text_at_cursor(p_text);
  3038. update();
  3039. }
  3040. Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
  3041. if (highlighted_word != String())
  3042. return CURSOR_POINTING_HAND;
  3043. int gutter = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width;
  3044. if ((completion_active && completion_rect.has_point(p_pos))) {
  3045. return CURSOR_ARROW;
  3046. }
  3047. if (p_pos.x < gutter) {
  3048. int row, col;
  3049. _get_mouse_pos(p_pos, row, col);
  3050. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  3051. // breakpoint icon
  3052. if (draw_breakpoint_gutter && p_pos.x > left_margin && p_pos.x <= left_margin + cache.breakpoint_gutter_width + 3) {
  3053. return CURSOR_POINTING_HAND;
  3054. }
  3055. // fold icon
  3056. int gutter_left = left_margin + cache.breakpoint_gutter_width + cache.line_number_w;
  3057. if (draw_fold_gutter && p_pos.x > gutter_left - 6 && p_pos.x <= gutter_left + cache.fold_gutter_width - 3) {
  3058. if (is_folded(row) || can_fold(row))
  3059. return CURSOR_POINTING_HAND;
  3060. else
  3061. return CURSOR_ARROW;
  3062. }
  3063. return CURSOR_ARROW;
  3064. } else {
  3065. int row, col;
  3066. _get_mouse_pos(p_pos, row, col);
  3067. // eol fold icon
  3068. if (is_folded(row)) {
  3069. int line_width = text.get_line_width(row);
  3070. line_width += cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width - cursor.x_ofs;
  3071. if (p_pos.x > line_width - 3 && p_pos.x <= line_width + cache.folded_eol_icon->get_width() + 3) {
  3072. return CURSOR_POINTING_HAND;
  3073. }
  3074. }
  3075. }
  3076. return CURSOR_IBEAM;
  3077. }
  3078. void TextEdit::set_text(String p_text) {
  3079. setting_text = true;
  3080. clear();
  3081. _insert_text_at_cursor(p_text);
  3082. clear_undo_history();
  3083. cursor.column = 0;
  3084. cursor.line = 0;
  3085. cursor.x_ofs = 0;
  3086. cursor.line_ofs = 0;
  3087. line_scroll_pos = 0;
  3088. cursor.last_fit_x = 0;
  3089. cursor_set_line(0);
  3090. cursor_set_column(0);
  3091. update();
  3092. setting_text = false;
  3093. _text_changed_emit();
  3094. //get_range()->set(0);
  3095. };
  3096. String TextEdit::get_text() {
  3097. String longthing;
  3098. int len = text.size();
  3099. for (int i = 0; i < len; i++) {
  3100. longthing += text[i];
  3101. if (i != len - 1)
  3102. longthing += "\n";
  3103. }
  3104. return longthing;
  3105. };
  3106. String TextEdit::get_text_for_lookup_completion() {
  3107. int row, col;
  3108. _get_mouse_pos(get_local_mouse_position(), row, col);
  3109. String longthing;
  3110. int len = text.size();
  3111. for (int i = 0; i < len; i++) {
  3112. if (i == row) {
  3113. longthing += text[i].substr(0, col);
  3114. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  3115. longthing += text[i].substr(col, text[i].size());
  3116. } else {
  3117. longthing += text[i];
  3118. }
  3119. if (i != len - 1)
  3120. longthing += "\n";
  3121. }
  3122. return longthing;
  3123. }
  3124. String TextEdit::get_text_for_completion() {
  3125. String longthing;
  3126. int len = text.size();
  3127. for (int i = 0; i < len; i++) {
  3128. if (i == cursor.line) {
  3129. longthing += text[i].substr(0, cursor.column);
  3130. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  3131. longthing += text[i].substr(cursor.column, text[i].size());
  3132. } else {
  3133. longthing += text[i];
  3134. }
  3135. if (i != len - 1)
  3136. longthing += "\n";
  3137. }
  3138. return longthing;
  3139. };
  3140. String TextEdit::get_line(int line) const {
  3141. if (line < 0 || line >= text.size())
  3142. return "";
  3143. return text[line];
  3144. };
  3145. void TextEdit::_clear() {
  3146. clear_undo_history();
  3147. text.clear();
  3148. cursor.column = 0;
  3149. cursor.line = 0;
  3150. cursor.x_ofs = 0;
  3151. cursor.line_ofs = 0;
  3152. line_scroll_pos = 0;
  3153. cursor.last_fit_x = 0;
  3154. }
  3155. void TextEdit::clear() {
  3156. setting_text = true;
  3157. _clear();
  3158. setting_text = false;
  3159. };
  3160. void TextEdit::set_readonly(bool p_readonly) {
  3161. readonly = p_readonly;
  3162. update();
  3163. }
  3164. bool TextEdit::is_readonly() const {
  3165. return readonly;
  3166. }
  3167. void TextEdit::set_wrap(bool p_wrap) {
  3168. wrap = p_wrap;
  3169. }
  3170. void TextEdit::set_max_chars(int p_max_chars) {
  3171. max_chars = p_max_chars;
  3172. }
  3173. void TextEdit::_reset_caret_blink_timer() {
  3174. if (caret_blink_enabled) {
  3175. caret_blink_timer->stop();
  3176. caret_blink_timer->start();
  3177. draw_caret = true;
  3178. update();
  3179. }
  3180. }
  3181. void TextEdit::_toggle_draw_caret() {
  3182. draw_caret = !draw_caret;
  3183. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  3184. update();
  3185. }
  3186. }
  3187. void TextEdit::_update_caches() {
  3188. cache.style_normal = get_stylebox("normal");
  3189. cache.style_focus = get_stylebox("focus");
  3190. cache.style_readonly = get_stylebox("read_only");
  3191. cache.completion_background_color = get_color("completion_background_color");
  3192. cache.completion_selected_color = get_color("completion_selected_color");
  3193. cache.completion_existing_color = get_color("completion_existing_color");
  3194. cache.completion_font_color = get_color("completion_font_color");
  3195. cache.font = get_font("font");
  3196. cache.caret_color = get_color("caret_color");
  3197. cache.caret_background_color = get_color("caret_background_color");
  3198. cache.line_number_color = get_color("line_number_color");
  3199. cache.font_color = get_color("font_color");
  3200. cache.font_selected_color = get_color("font_selected_color");
  3201. cache.keyword_color = get_color("keyword_color");
  3202. cache.function_color = get_color("function_color");
  3203. cache.member_variable_color = get_color("member_variable_color");
  3204. cache.number_color = get_color("number_color");
  3205. cache.selection_color = get_color("selection_color");
  3206. cache.mark_color = get_color("mark_color");
  3207. cache.current_line_color = get_color("current_line_color");
  3208. cache.line_length_guideline_color = get_color("line_length_guideline_color");
  3209. cache.breakpoint_color = get_color("breakpoint_color");
  3210. cache.code_folding_color = get_color("code_folding_color");
  3211. cache.brace_mismatch_color = get_color("brace_mismatch_color");
  3212. cache.word_highlighted_color = get_color("word_highlighted_color");
  3213. cache.search_result_color = get_color("search_result_color");
  3214. cache.search_result_border_color = get_color("search_result_border_color");
  3215. cache.symbol_color = get_color("symbol_color");
  3216. cache.background_color = get_color("background_color");
  3217. cache.line_spacing = get_constant("line_spacing");
  3218. cache.row_height = cache.font->get_height() + cache.line_spacing;
  3219. cache.tab_icon = get_icon("tab");
  3220. cache.folded_icon = get_icon("GuiTreeArrowRight", "EditorIcons");
  3221. cache.can_fold_icon = get_icon("GuiTreeArrowDown", "EditorIcons");
  3222. cache.folded_eol_icon = get_icon("GuiEllipsis", "EditorIcons");
  3223. text.set_font(cache.font);
  3224. }
  3225. void TextEdit::clear_colors() {
  3226. keywords.clear();
  3227. color_regions.clear();
  3228. text.clear_caches();
  3229. }
  3230. void TextEdit::add_keyword_color(const String &p_keyword, const Color &p_color) {
  3231. keywords[p_keyword] = p_color;
  3232. update();
  3233. }
  3234. void TextEdit::add_color_region(const String &p_begin_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  3235. color_regions.push_back(ColorRegion(p_begin_key, p_end_key, p_color, p_line_only));
  3236. text.clear_caches();
  3237. update();
  3238. }
  3239. void TextEdit::set_syntax_coloring(bool p_enabled) {
  3240. syntax_coloring = p_enabled;
  3241. update();
  3242. }
  3243. bool TextEdit::is_syntax_coloring_enabled() const {
  3244. return syntax_coloring;
  3245. }
  3246. void TextEdit::set_auto_indent(bool p_auto_indent) {
  3247. auto_indent = p_auto_indent;
  3248. }
  3249. void TextEdit::cut() {
  3250. if (!selection.active) {
  3251. String clipboard = text[cursor.line];
  3252. OS::get_singleton()->set_clipboard(clipboard);
  3253. cursor_set_line(cursor.line);
  3254. cursor_set_column(0);
  3255. _remove_text(cursor.line, 0, cursor.line, text[cursor.line].length());
  3256. backspace_at_cursor();
  3257. update();
  3258. cursor_set_line(cursor.line + 1);
  3259. cut_copy_line = clipboard;
  3260. } else {
  3261. String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3262. OS::get_singleton()->set_clipboard(clipboard);
  3263. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3264. cursor_set_line(selection.from_line); // set afterwards else it causes the view to be offset
  3265. cursor_set_column(selection.from_column);
  3266. selection.active = false;
  3267. selection.selecting_mode = Selection::MODE_NONE;
  3268. update();
  3269. cut_copy_line = "";
  3270. }
  3271. }
  3272. void TextEdit::copy() {
  3273. if (!selection.active) {
  3274. String clipboard = _base_get_text(cursor.line, 0, cursor.line, text[cursor.line].length());
  3275. OS::get_singleton()->set_clipboard(clipboard);
  3276. cut_copy_line = clipboard;
  3277. } else {
  3278. String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3279. OS::get_singleton()->set_clipboard(clipboard);
  3280. cut_copy_line = "";
  3281. }
  3282. }
  3283. void TextEdit::paste() {
  3284. String clipboard = OS::get_singleton()->get_clipboard();
  3285. if (selection.active) {
  3286. selection.active = false;
  3287. selection.selecting_mode = Selection::MODE_NONE;
  3288. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3289. cursor_set_line(selection.from_line);
  3290. cursor_set_column(selection.from_column);
  3291. } else if (!cut_copy_line.empty() && cut_copy_line == clipboard) {
  3292. cursor_set_column(0);
  3293. String ins = "\n";
  3294. clipboard += ins;
  3295. }
  3296. _insert_text_at_cursor(clipboard);
  3297. update();
  3298. }
  3299. void TextEdit::select_all() {
  3300. if (text.size() == 1 && text[0].length() == 0)
  3301. return;
  3302. selection.active = true;
  3303. selection.from_line = 0;
  3304. selection.from_column = 0;
  3305. selection.selecting_line = 0;
  3306. selection.selecting_column = 0;
  3307. selection.to_line = text.size() - 1;
  3308. selection.to_column = text[selection.to_line].length();
  3309. selection.selecting_mode = Selection::MODE_SHIFT;
  3310. selection.shiftclick_left = true;
  3311. cursor_set_line(selection.to_line, false);
  3312. cursor_set_column(selection.to_column, false);
  3313. update();
  3314. }
  3315. void TextEdit::deselect() {
  3316. selection.active = false;
  3317. update();
  3318. }
  3319. void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3320. if (p_from_line >= text.size())
  3321. p_from_line = text.size() - 1;
  3322. if (p_from_column >= text[p_from_line].length())
  3323. p_from_column = text[p_from_line].length();
  3324. if (p_to_line >= text.size())
  3325. p_to_line = text.size() - 1;
  3326. if (p_to_column >= text[p_to_line].length())
  3327. p_to_column = text[p_to_line].length();
  3328. selection.from_line = p_from_line;
  3329. selection.from_column = p_from_column;
  3330. selection.to_line = p_to_line;
  3331. selection.to_column = p_to_column;
  3332. selection.active = true;
  3333. if (selection.from_line == selection.to_line) {
  3334. if (selection.from_column == selection.to_column) {
  3335. selection.active = false;
  3336. } else if (selection.from_column > selection.to_column) {
  3337. selection.shiftclick_left = false;
  3338. SWAP(selection.from_column, selection.to_column);
  3339. } else {
  3340. selection.shiftclick_left = true;
  3341. }
  3342. } else if (selection.from_line > selection.to_line) {
  3343. selection.shiftclick_left = false;
  3344. SWAP(selection.from_line, selection.to_line);
  3345. SWAP(selection.from_column, selection.to_column);
  3346. } else {
  3347. selection.shiftclick_left = true;
  3348. }
  3349. update();
  3350. }
  3351. void TextEdit::swap_lines(int line1, int line2) {
  3352. String tmp = get_line(line1);
  3353. String tmp2 = get_line(line2);
  3354. set_line(line2, tmp);
  3355. set_line(line1, tmp2);
  3356. }
  3357. bool TextEdit::is_selection_active() const {
  3358. return selection.active;
  3359. }
  3360. int TextEdit::get_selection_from_line() const {
  3361. ERR_FAIL_COND_V(!selection.active, -1);
  3362. return selection.from_line;
  3363. }
  3364. int TextEdit::get_selection_from_column() const {
  3365. ERR_FAIL_COND_V(!selection.active, -1);
  3366. return selection.from_column;
  3367. }
  3368. int TextEdit::get_selection_to_line() const {
  3369. ERR_FAIL_COND_V(!selection.active, -1);
  3370. return selection.to_line;
  3371. }
  3372. int TextEdit::get_selection_to_column() const {
  3373. ERR_FAIL_COND_V(!selection.active, -1);
  3374. return selection.to_column;
  3375. }
  3376. String TextEdit::get_selection_text() const {
  3377. if (!selection.active)
  3378. return "";
  3379. return _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3380. }
  3381. String TextEdit::get_word_under_cursor() const {
  3382. int prev_cc = cursor.column;
  3383. while (prev_cc > 0) {
  3384. bool is_char = _is_text_char(text[cursor.line][prev_cc - 1]);
  3385. if (!is_char)
  3386. break;
  3387. --prev_cc;
  3388. }
  3389. int next_cc = cursor.column;
  3390. while (next_cc < text[cursor.line].length()) {
  3391. bool is_char = _is_text_char(text[cursor.line][next_cc]);
  3392. if (!is_char)
  3393. break;
  3394. ++next_cc;
  3395. }
  3396. if (prev_cc == cursor.column || next_cc == cursor.column)
  3397. return "";
  3398. return text[cursor.line].substr(prev_cc, next_cc - prev_cc);
  3399. }
  3400. void TextEdit::set_search_text(const String &p_search_text) {
  3401. search_text = p_search_text;
  3402. }
  3403. void TextEdit::set_search_flags(uint32_t p_flags) {
  3404. search_flags = p_flags;
  3405. }
  3406. void TextEdit::set_current_search_result(int line, int col) {
  3407. search_result_line = line;
  3408. search_result_col = col;
  3409. update();
  3410. }
  3411. void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
  3412. highlight_all_occurrences = p_enabled;
  3413. update();
  3414. }
  3415. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  3416. return highlight_all_occurrences;
  3417. }
  3418. int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) {
  3419. int col = -1;
  3420. if (p_key.length() > 0 && p_search.length() > 0) {
  3421. if (p_from_column < 0 || p_from_column > p_search.length()) {
  3422. p_from_column = 0;
  3423. }
  3424. while (col == -1 && p_from_column <= p_search.length()) {
  3425. if (p_search_flags & SEARCH_MATCH_CASE) {
  3426. col = p_search.find(p_key, p_from_column);
  3427. } else {
  3428. col = p_search.findn(p_key, p_from_column);
  3429. }
  3430. // whole words only
  3431. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
  3432. p_from_column = col;
  3433. if (col > 0 && _is_text_char(p_search[col - 1])) {
  3434. col = -1;
  3435. } else if ((col + p_key.length()) < p_search.length() && _is_text_char(p_search[col + p_key.length()])) {
  3436. col = -1;
  3437. }
  3438. }
  3439. p_from_column += 1;
  3440. }
  3441. }
  3442. return col;
  3443. }
  3444. PoolVector<int> TextEdit::_search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
  3445. int col, line;
  3446. if (search(p_key, p_search_flags, p_from_line, p_from_column, col, line)) {
  3447. PoolVector<int> result;
  3448. result.resize(2);
  3449. result.set(0, line);
  3450. result.set(1, col);
  3451. return result;
  3452. } else {
  3453. return PoolVector<int>();
  3454. }
  3455. }
  3456. 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 {
  3457. if (p_key.length() == 0)
  3458. return false;
  3459. ERR_FAIL_INDEX_V(p_from_line, text.size(), false);
  3460. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, false);
  3461. //search through the whole documment, but start by current line
  3462. int line = p_from_line;
  3463. int pos = -1;
  3464. for (int i = 0; i < text.size() + 1; i++) {
  3465. //backwards is broken...
  3466. //int idx=(p_search_flags&SEARCH_BACKWARDS)?(text.size()-i):i; //do backwards seearch
  3467. if (line < 0) {
  3468. line = text.size() - 1;
  3469. }
  3470. if (line == text.size()) {
  3471. line = 0;
  3472. }
  3473. String text_line = text[line];
  3474. int from_column = 0;
  3475. if (line == p_from_line) {
  3476. if (i == text.size()) {
  3477. //wrapped
  3478. if (p_search_flags & SEARCH_BACKWARDS) {
  3479. from_column = text_line.length();
  3480. } else {
  3481. from_column = 0;
  3482. }
  3483. } else {
  3484. from_column = p_from_column;
  3485. }
  3486. } else {
  3487. if (p_search_flags & SEARCH_BACKWARDS)
  3488. from_column = text_line.length() - 1;
  3489. else
  3490. from_column = 0;
  3491. }
  3492. pos = -1;
  3493. int pos_from = 0;
  3494. int last_pos = -1;
  3495. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) {
  3496. if (p_search_flags & SEARCH_BACKWARDS) {
  3497. if (last_pos > from_column)
  3498. break;
  3499. pos = last_pos;
  3500. } else {
  3501. if (last_pos >= from_column) {
  3502. pos = last_pos;
  3503. break;
  3504. }
  3505. }
  3506. pos_from = last_pos + p_key.length();
  3507. }
  3508. if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) {
  3509. //validate for whole words
  3510. if (pos > 0 && _is_text_char(text_line[pos - 1]))
  3511. pos = -1;
  3512. else if (_is_text_char(text_line[pos + p_key.length()]))
  3513. pos = -1;
  3514. }
  3515. if (pos != -1)
  3516. break;
  3517. if (p_search_flags & SEARCH_BACKWARDS)
  3518. line--;
  3519. else
  3520. line++;
  3521. }
  3522. if (pos == -1) {
  3523. r_line = -1;
  3524. r_column = -1;
  3525. return false;
  3526. }
  3527. r_line = line;
  3528. r_column = pos;
  3529. return true;
  3530. }
  3531. void TextEdit::_cursor_changed_emit() {
  3532. emit_signal("cursor_changed");
  3533. cursor_changed_dirty = false;
  3534. }
  3535. void TextEdit::_text_changed_emit() {
  3536. emit_signal("text_changed");
  3537. text_changed_dirty = false;
  3538. }
  3539. void TextEdit::set_line_as_marked(int p_line, bool p_marked) {
  3540. ERR_FAIL_INDEX(p_line, text.size());
  3541. text.set_marked(p_line, p_marked);
  3542. update();
  3543. }
  3544. bool TextEdit::is_line_set_as_breakpoint(int p_line) const {
  3545. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3546. return text.is_breakpoint(p_line);
  3547. }
  3548. void TextEdit::set_line_as_breakpoint(int p_line, bool p_breakpoint) {
  3549. ERR_FAIL_INDEX(p_line, text.size());
  3550. text.set_breakpoint(p_line, p_breakpoint);
  3551. update();
  3552. }
  3553. void TextEdit::get_breakpoints(List<int> *p_breakpoints) const {
  3554. for (int i = 0; i < text.size(); i++) {
  3555. if (text.is_breakpoint(i))
  3556. p_breakpoints->push_back(i);
  3557. }
  3558. }
  3559. void TextEdit::set_line_as_hidden(int p_line, bool p_hidden) {
  3560. ERR_FAIL_INDEX(p_line, text.size());
  3561. if (is_hiding_enabled() || !p_hidden)
  3562. text.set_hidden(p_line, p_hidden);
  3563. update();
  3564. }
  3565. bool TextEdit::is_line_hidden(int p_line) const {
  3566. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3567. return text.is_hidden(p_line);
  3568. }
  3569. void TextEdit::fold_all_lines() {
  3570. for (int i = 0; i < text.size(); i++) {
  3571. fold_line(i);
  3572. }
  3573. _update_scrollbars();
  3574. update();
  3575. }
  3576. void TextEdit::unhide_all_lines() {
  3577. for (int i = 0; i < text.size(); i++) {
  3578. text.set_hidden(i, false);
  3579. }
  3580. _update_scrollbars();
  3581. update();
  3582. }
  3583. int TextEdit::num_lines_from(int p_line_from, int unhidden_amount) const {
  3584. // returns the number of hidden and unhidden lines from p_line_from to p_line_from + amount of visible lines
  3585. ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(unhidden_amount));
  3586. if (!is_hiding_enabled())
  3587. return ABS(unhidden_amount);
  3588. int num_visible = 0;
  3589. int num_total = 0;
  3590. if (unhidden_amount >= 0) {
  3591. for (int i = p_line_from; i < text.size(); i++) {
  3592. num_total++;
  3593. if (!is_line_hidden(i))
  3594. num_visible++;
  3595. if (num_visible >= unhidden_amount)
  3596. break;
  3597. }
  3598. } else {
  3599. unhidden_amount = ABS(unhidden_amount);
  3600. for (int i = p_line_from; i >= 0; i--) {
  3601. num_total++;
  3602. if (!is_line_hidden(i))
  3603. num_visible++;
  3604. if (num_visible >= unhidden_amount)
  3605. break;
  3606. }
  3607. }
  3608. return num_total;
  3609. }
  3610. int TextEdit::get_indent_level(int p_line) const {
  3611. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3612. // counts number of tabs and spaces before line starts
  3613. int tab_count = 0;
  3614. int whitespace_count = 0;
  3615. int line_length = text[p_line].size();
  3616. for (int i = 0; i < line_length - 1; i++) {
  3617. if (text[p_line][i] == '\t') {
  3618. tab_count++;
  3619. } else if (text[p_line][i] == ' ') {
  3620. whitespace_count++;
  3621. } else if (text[p_line][i] == '#') {
  3622. break;
  3623. } else {
  3624. break;
  3625. }
  3626. }
  3627. return tab_count + whitespace_count / indent_size;
  3628. }
  3629. bool TextEdit::can_fold(int p_line) const {
  3630. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3631. if (!is_hiding_enabled())
  3632. return false;
  3633. if (p_line + 1 >= text.size())
  3634. return false;
  3635. if (text[p_line].size() == 0)
  3636. return false;
  3637. if (is_folded(p_line))
  3638. return false;
  3639. if (is_line_hidden(p_line))
  3640. return false;
  3641. int start_indent = get_indent_level(p_line);
  3642. for (int i = p_line + 1; i < text.size(); i++) {
  3643. if (text[i].size() == 0)
  3644. continue;
  3645. int next_indent = get_indent_level(i);
  3646. if (next_indent > start_indent)
  3647. return true;
  3648. else
  3649. return false;
  3650. }
  3651. return false;
  3652. }
  3653. bool TextEdit::is_folded(int p_line) const {
  3654. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3655. if (p_line + 1 >= text.size())
  3656. return false;
  3657. if (!is_line_hidden(p_line) && is_line_hidden(p_line + 1))
  3658. return true;
  3659. return false;
  3660. }
  3661. void TextEdit::fold_line(int p_line) {
  3662. ERR_FAIL_INDEX(p_line, text.size());
  3663. if (!is_hiding_enabled())
  3664. return;
  3665. if (!can_fold(p_line))
  3666. return;
  3667. // hide lines below this one
  3668. int start_indent = get_indent_level(p_line);
  3669. int last_line = start_indent;
  3670. for (int i = p_line + 1; i < text.size(); i++) {
  3671. if (text[i].strip_edges().size() != 0) {
  3672. if (get_indent_level(i) > start_indent) {
  3673. last_line = i;
  3674. } else {
  3675. break;
  3676. }
  3677. }
  3678. }
  3679. for (int i = p_line + 1; i <= last_line; i++) {
  3680. set_line_as_hidden(i, true);
  3681. }
  3682. // fix selection
  3683. if (is_selection_active()) {
  3684. if (is_line_hidden(selection.from_line) && is_line_hidden(selection.to_line)) {
  3685. deselect();
  3686. } else if (is_line_hidden(selection.from_line)) {
  3687. select(p_line, 9999, selection.to_line, selection.to_column);
  3688. } else if (is_line_hidden(selection.to_line)) {
  3689. select(selection.from_line, selection.from_column, p_line, 9999);
  3690. }
  3691. }
  3692. // reset cursor
  3693. if (is_line_hidden(cursor.line)) {
  3694. cursor_set_line(p_line, false, false);
  3695. cursor_set_column(get_line(p_line).length(), false);
  3696. }
  3697. _update_scrollbars();
  3698. update();
  3699. }
  3700. void TextEdit::unfold_line(int p_line) {
  3701. ERR_FAIL_INDEX(p_line, text.size());
  3702. if (!is_folded(p_line) && !is_line_hidden(p_line))
  3703. return;
  3704. int fold_start = p_line;
  3705. for (fold_start = p_line; fold_start > 0; fold_start--) {
  3706. if (is_folded(fold_start))
  3707. break;
  3708. }
  3709. fold_start = is_folded(fold_start) ? fold_start : p_line;
  3710. for (int i = fold_start + 1; i < text.size(); i++) {
  3711. if (is_line_hidden(i)) {
  3712. set_line_as_hidden(i, false);
  3713. } else {
  3714. break;
  3715. }
  3716. }
  3717. _update_scrollbars();
  3718. update();
  3719. }
  3720. void TextEdit::toggle_fold_line(int p_line) {
  3721. ERR_FAIL_INDEX(p_line, text.size());
  3722. if (!is_folded(p_line))
  3723. fold_line(p_line);
  3724. else
  3725. unfold_line(p_line);
  3726. }
  3727. int TextEdit::get_line_count() const {
  3728. return text.size();
  3729. }
  3730. void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
  3731. ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
  3732. bool insert = p_op.type == TextOperation::TYPE_INSERT;
  3733. if (p_reverse)
  3734. insert = !insert;
  3735. if (insert) {
  3736. int check_line;
  3737. int check_column;
  3738. _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
  3739. ERR_FAIL_COND(check_line != p_op.to_line); // BUG
  3740. ERR_FAIL_COND(check_column != p_op.to_column); // BUG
  3741. } else {
  3742. _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
  3743. }
  3744. }
  3745. void TextEdit::_clear_redo() {
  3746. if (undo_stack_pos == NULL)
  3747. return; //nothing to clear
  3748. _push_current_op();
  3749. while (undo_stack_pos) {
  3750. List<TextOperation>::Element *elem = undo_stack_pos;
  3751. undo_stack_pos = undo_stack_pos->next();
  3752. undo_stack.erase(elem);
  3753. }
  3754. }
  3755. void TextEdit::undo() {
  3756. _push_current_op();
  3757. if (undo_stack_pos == NULL) {
  3758. if (!undo_stack.size())
  3759. return; //nothing to undo
  3760. undo_stack_pos = undo_stack.back();
  3761. } else if (undo_stack_pos == undo_stack.front())
  3762. return; // at the bottom of the undo stack
  3763. else
  3764. undo_stack_pos = undo_stack_pos->prev();
  3765. TextOperation op = undo_stack_pos->get();
  3766. _do_text_op(op, true);
  3767. current_op.version = op.prev_version;
  3768. if (undo_stack_pos->get().chain_backward) {
  3769. while (true) {
  3770. ERR_BREAK(!undo_stack_pos->prev());
  3771. undo_stack_pos = undo_stack_pos->prev();
  3772. op = undo_stack_pos->get();
  3773. _do_text_op(op, true);
  3774. current_op.version = op.prev_version;
  3775. if (undo_stack_pos->get().chain_forward) {
  3776. break;
  3777. }
  3778. }
  3779. }
  3780. if (undo_stack_pos->get().type == TextOperation::TYPE_REMOVE) {
  3781. cursor_set_line(undo_stack_pos->get().to_line);
  3782. cursor_set_column(undo_stack_pos->get().to_column);
  3783. _cancel_code_hint();
  3784. } else {
  3785. cursor_set_line(undo_stack_pos->get().from_line);
  3786. cursor_set_column(undo_stack_pos->get().from_column);
  3787. }
  3788. update();
  3789. }
  3790. void TextEdit::redo() {
  3791. _push_current_op();
  3792. if (undo_stack_pos == NULL)
  3793. return; //nothing to do.
  3794. TextOperation op = undo_stack_pos->get();
  3795. _do_text_op(op, false);
  3796. current_op.version = op.version;
  3797. if (undo_stack_pos->get().chain_forward) {
  3798. while (true) {
  3799. ERR_BREAK(!undo_stack_pos->next());
  3800. undo_stack_pos = undo_stack_pos->next();
  3801. op = undo_stack_pos->get();
  3802. _do_text_op(op, false);
  3803. current_op.version = op.version;
  3804. if (undo_stack_pos->get().chain_backward)
  3805. break;
  3806. }
  3807. }
  3808. cursor_set_line(undo_stack_pos->get().to_line);
  3809. cursor_set_column(undo_stack_pos->get().to_column);
  3810. undo_stack_pos = undo_stack_pos->next();
  3811. update();
  3812. }
  3813. void TextEdit::clear_undo_history() {
  3814. saved_version = 0;
  3815. current_op.type = TextOperation::TYPE_NONE;
  3816. undo_stack_pos = NULL;
  3817. undo_stack.clear();
  3818. }
  3819. void TextEdit::begin_complex_operation() {
  3820. _push_current_op();
  3821. next_operation_is_complex = true;
  3822. }
  3823. void TextEdit::end_complex_operation() {
  3824. _push_current_op();
  3825. ERR_FAIL_COND(undo_stack.size() == 0);
  3826. if (undo_stack.back()->get().chain_forward) {
  3827. undo_stack.back()->get().chain_forward = false;
  3828. return;
  3829. }
  3830. undo_stack.back()->get().chain_backward = true;
  3831. }
  3832. void TextEdit::_push_current_op() {
  3833. if (current_op.type == TextOperation::TYPE_NONE)
  3834. return; // do nothing
  3835. if (next_operation_is_complex) {
  3836. current_op.chain_forward = true;
  3837. next_operation_is_complex = false;
  3838. }
  3839. undo_stack.push_back(current_op);
  3840. current_op.type = TextOperation::TYPE_NONE;
  3841. current_op.text = "";
  3842. current_op.chain_forward = false;
  3843. }
  3844. void TextEdit::set_indent_using_spaces(const bool p_use_spaces) {
  3845. indent_using_spaces = p_use_spaces;
  3846. }
  3847. bool TextEdit::is_indent_using_spaces() const {
  3848. return indent_using_spaces;
  3849. }
  3850. void TextEdit::set_indent_size(const int p_size) {
  3851. ERR_FAIL_COND(p_size <= 0);
  3852. indent_size = p_size;
  3853. text.set_indent_size(p_size);
  3854. space_indent = "";
  3855. for (int i = 0; i < p_size; i++) {
  3856. space_indent += " ";
  3857. }
  3858. update();
  3859. }
  3860. void TextEdit::set_draw_tabs(bool p_draw) {
  3861. draw_tabs = p_draw;
  3862. }
  3863. bool TextEdit::is_drawing_tabs() const {
  3864. return draw_tabs;
  3865. }
  3866. void TextEdit::set_override_selected_font_color(bool p_override_selected_font_color) {
  3867. override_selected_font_color = p_override_selected_font_color;
  3868. }
  3869. bool TextEdit::is_overriding_selected_font_color() const {
  3870. return override_selected_font_color;
  3871. }
  3872. void TextEdit::set_insert_mode(bool p_enabled) {
  3873. insert_mode = p_enabled;
  3874. update();
  3875. }
  3876. bool TextEdit::is_insert_mode() const {
  3877. return insert_mode;
  3878. }
  3879. bool TextEdit::is_insert_text_operation() {
  3880. return (current_op.type == TextOperation::TYPE_INSERT);
  3881. }
  3882. uint32_t TextEdit::get_version() const {
  3883. return current_op.version;
  3884. }
  3885. uint32_t TextEdit::get_saved_version() const {
  3886. return saved_version;
  3887. }
  3888. void TextEdit::tag_saved_version() {
  3889. saved_version = get_version();
  3890. }
  3891. int TextEdit::get_v_scroll() const {
  3892. return v_scroll->get_value();
  3893. }
  3894. void TextEdit::set_v_scroll(int p_scroll) {
  3895. if (p_scroll < 0) {
  3896. p_scroll = 0;
  3897. }
  3898. if (!scroll_past_end_of_file_enabled) {
  3899. if (p_scroll + get_visible_rows() > get_total_unhidden_rows()) {
  3900. int num_rows = num_lines_from(CLAMP(p_scroll, 0, text.size() - 1), MIN(get_visible_rows(), text.size() - 1 - p_scroll));
  3901. p_scroll = text.size() - num_rows;
  3902. }
  3903. }
  3904. v_scroll->set_value(p_scroll);
  3905. cursor.line_ofs = num_lines_from(0, p_scroll);
  3906. line_scroll_pos = p_scroll;
  3907. }
  3908. int TextEdit::get_h_scroll() const {
  3909. return h_scroll->get_value();
  3910. }
  3911. void TextEdit::set_h_scroll(int p_scroll) {
  3912. h_scroll->set_value(p_scroll);
  3913. }
  3914. void TextEdit::set_smooth_scroll_enabled(bool p_enable) {
  3915. v_scroll->set_smooth_scroll_enabled(p_enable);
  3916. smooth_scroll_enabled = p_enable;
  3917. }
  3918. bool TextEdit::is_smooth_scroll_enabled() const {
  3919. return smooth_scroll_enabled;
  3920. }
  3921. void TextEdit::set_v_scroll_speed(float p_speed) {
  3922. v_scroll_speed = p_speed;
  3923. }
  3924. float TextEdit::get_v_scroll_speed() const {
  3925. return v_scroll_speed;
  3926. }
  3927. void TextEdit::set_completion(bool p_enabled, const Vector<String> &p_prefixes) {
  3928. completion_prefixes.clear();
  3929. completion_enabled = p_enabled;
  3930. for (int i = 0; i < p_prefixes.size(); i++)
  3931. completion_prefixes.insert(p_prefixes[i]);
  3932. }
  3933. void TextEdit::_confirm_completion() {
  3934. begin_complex_operation();
  3935. _remove_text(cursor.line, cursor.column - completion_base.length(), cursor.line, cursor.column);
  3936. cursor_set_column(cursor.column - completion_base.length(), false);
  3937. insert_text_at_cursor(completion_current);
  3938. if (completion_current.ends_with("(") && auto_brace_completion_enabled) {
  3939. insert_text_at_cursor(")");
  3940. cursor.column--;
  3941. }
  3942. end_complex_operation();
  3943. _cancel_completion();
  3944. }
  3945. void TextEdit::_cancel_code_hint() {
  3946. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
  3947. raised_from_completion = false;
  3948. completion_hint = "";
  3949. update();
  3950. }
  3951. void TextEdit::_cancel_completion() {
  3952. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
  3953. raised_from_completion = false;
  3954. if (!completion_active)
  3955. return;
  3956. completion_active = false;
  3957. completion_forced = false;
  3958. update();
  3959. }
  3960. static bool _is_completable(CharType c) {
  3961. return !_is_symbol(c) || c == '"' || c == '\'';
  3962. }
  3963. void TextEdit::_update_completion_candidates() {
  3964. String l = text[cursor.line];
  3965. int cofs = CLAMP(cursor.column, 0, l.length());
  3966. String s;
  3967. //look for keywords first
  3968. bool inquote = false;
  3969. int first_quote = -1;
  3970. int c = cofs - 1;
  3971. while (c >= 0) {
  3972. if (l[c] == '"' || l[c] == '\'') {
  3973. inquote = !inquote;
  3974. if (first_quote == -1)
  3975. first_quote = c;
  3976. }
  3977. c--;
  3978. }
  3979. bool pre_keyword = false;
  3980. bool cancel = false;
  3981. //print_line("inquote: "+itos(inquote)+"first quote "+itos(first_quote)+" cofs-1 "+itos(cofs-1));
  3982. if (!inquote && first_quote == cofs - 1) {
  3983. //no completion here
  3984. //print_line("cancel!");
  3985. cancel = true;
  3986. } else if (inquote && first_quote != -1) {
  3987. s = l.substr(first_quote, cofs - first_quote);
  3988. //print_line("s: 1"+s);
  3989. } else if (cofs > 0 && l[cofs - 1] == ' ') {
  3990. int kofs = cofs - 1;
  3991. String kw;
  3992. while (kofs >= 0 && l[kofs] == ' ')
  3993. kofs--;
  3994. while (kofs >= 0 && l[kofs] > 32 && _is_completable(l[kofs])) {
  3995. kw = String::chr(l[kofs]) + kw;
  3996. kofs--;
  3997. }
  3998. pre_keyword = keywords.has(kw);
  3999. //print_line("KW "+kw+"? "+itos(pre_keyword));
  4000. } else {
  4001. while (cofs > 0 && l[cofs - 1] > 32 && _is_completable(l[cofs - 1])) {
  4002. s = String::chr(l[cofs - 1]) + s;
  4003. if (l[cofs - 1] == '\'' || l[cofs - 1] == '"' || l[cofs - 1] == '$')
  4004. break;
  4005. cofs--;
  4006. }
  4007. }
  4008. if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_forced) {
  4009. cancel = true;
  4010. }
  4011. update();
  4012. bool prev_is_prefix = false;
  4013. if (cofs > 0 && completion_prefixes.has(String::chr(l[cofs - 1])))
  4014. prev_is_prefix = true;
  4015. if (cofs > 1 && l[cofs - 1] == ' ' && completion_prefixes.has(String::chr(l[cofs - 2]))) //check with one space before prefix, to allow indent
  4016. prev_is_prefix = true;
  4017. if (cancel || (!pre_keyword && s == "" && (cofs == 0 || !prev_is_prefix))) {
  4018. //none to complete, cancel
  4019. _cancel_completion();
  4020. return;
  4021. }
  4022. completion_options.clear();
  4023. completion_index = 0;
  4024. completion_base = s;
  4025. Vector<float> sim_cache;
  4026. bool single_quote = s.begins_with("'");
  4027. for (int i = 0; i < completion_strings.size(); i++) {
  4028. if (single_quote && completion_strings[i].is_quoted()) {
  4029. completion_strings[i] = completion_strings[i].unquote().quote("'");
  4030. }
  4031. if (s == completion_strings[i]) {
  4032. // A perfect match, stop completion
  4033. _cancel_completion();
  4034. return;
  4035. }
  4036. if (s.is_subsequence_ofi(completion_strings[i])) {
  4037. // don't remove duplicates if no input is provided
  4038. if (s != "" && completion_options.find(completion_strings[i]) != -1) {
  4039. continue;
  4040. }
  4041. // Calculate the similarity to keep completions in good order
  4042. float similarity;
  4043. if (completion_strings[i].to_lower().begins_with(s.to_lower())) {
  4044. // Substrings are the best candidates
  4045. similarity = 1.1;
  4046. } else {
  4047. // Otherwise compute the similarity
  4048. similarity = s.to_lower().similarity(completion_strings[i].to_lower());
  4049. }
  4050. int comp_size = completion_options.size();
  4051. if (comp_size == 0) {
  4052. completion_options.push_back(completion_strings[i]);
  4053. sim_cache.push_back(similarity);
  4054. } else {
  4055. float comp_sim;
  4056. int pos = 0;
  4057. do {
  4058. comp_sim = sim_cache[pos++];
  4059. } while (pos < comp_size && similarity < comp_sim);
  4060. pos = similarity > comp_sim ? pos - 1 : pos; // Pos will be off by one
  4061. completion_options.insert(pos, completion_strings[i]);
  4062. sim_cache.insert(pos, similarity);
  4063. }
  4064. }
  4065. }
  4066. if (completion_options.size() == 0) {
  4067. //no options to complete, cancel
  4068. _cancel_completion();
  4069. return;
  4070. }
  4071. // The top of the list is the best match
  4072. completion_current = completion_options[0];
  4073. completion_enabled = true;
  4074. }
  4075. void TextEdit::query_code_comple() {
  4076. String l = text[cursor.line];
  4077. int ofs = CLAMP(cursor.column, 0, l.length());
  4078. bool inquote = false;
  4079. int c = ofs - 1;
  4080. while (c >= 0) {
  4081. if (l[c] == '"' || l[c] == '\'')
  4082. inquote = !inquote;
  4083. c--;
  4084. }
  4085. if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1]))))
  4086. emit_signal("request_completion");
  4087. else if (ofs > 1 && l[ofs - 1] == ' ' && completion_prefixes.has(String::chr(l[ofs - 2]))) //make it work with a space too, it's good enough
  4088. emit_signal("request_completion");
  4089. }
  4090. void TextEdit::set_code_hint(const String &p_hint) {
  4091. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
  4092. raised_from_completion = true;
  4093. completion_hint = p_hint;
  4094. completion_hint_offset = -0xFFFF;
  4095. update();
  4096. }
  4097. void TextEdit::code_complete(const Vector<String> &p_strings, bool p_forced) {
  4098. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
  4099. raised_from_completion = true;
  4100. completion_strings = p_strings;
  4101. completion_active = true;
  4102. completion_forced = p_forced;
  4103. completion_current = "";
  4104. completion_index = 0;
  4105. _update_completion_candidates();
  4106. //
  4107. }
  4108. String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
  4109. int row, col;
  4110. _get_mouse_pos(p_pos, row, col);
  4111. String s = text[row];
  4112. if (s.length() == 0)
  4113. return "";
  4114. int beg = CLAMP(col, 0, s.length());
  4115. int end = beg;
  4116. if (s[beg] > 32 || beg == s.length()) {
  4117. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  4118. bool inside_quotes = false;
  4119. int qbegin = 0, qend = 0;
  4120. for (int i = 0; i < s.length(); i++) {
  4121. if (s[i] == '"') {
  4122. if (inside_quotes) {
  4123. qend = i;
  4124. inside_quotes = false;
  4125. if (col >= qbegin && col <= qend) {
  4126. return s.substr(qbegin, qend - qbegin);
  4127. }
  4128. } else {
  4129. qbegin = i + 1;
  4130. inside_quotes = true;
  4131. }
  4132. }
  4133. }
  4134. while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) {
  4135. beg--;
  4136. }
  4137. while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) {
  4138. end++;
  4139. }
  4140. if (end < s.length())
  4141. end += 1;
  4142. return s.substr(beg, end - beg);
  4143. }
  4144. return String();
  4145. }
  4146. String TextEdit::get_tooltip(const Point2 &p_pos) const {
  4147. if (!tooltip_obj)
  4148. return Control::get_tooltip(p_pos);
  4149. int row, col;
  4150. _get_mouse_pos(p_pos, row, col);
  4151. String s = text[row];
  4152. if (s.length() == 0)
  4153. return Control::get_tooltip(p_pos);
  4154. int beg = CLAMP(col, 0, s.length());
  4155. int end = beg;
  4156. if (s[beg] > 32 || beg == s.length()) {
  4157. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  4158. while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) {
  4159. beg--;
  4160. }
  4161. while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) {
  4162. end++;
  4163. }
  4164. if (end < s.length())
  4165. end += 1;
  4166. String tt = tooltip_obj->call(tooltip_func, s.substr(beg, end - beg), tooltip_ud);
  4167. return tt;
  4168. }
  4169. return Control::get_tooltip(p_pos);
  4170. }
  4171. void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata) {
  4172. tooltip_obj = p_obj;
  4173. tooltip_func = p_function;
  4174. tooltip_ud = p_udata;
  4175. }
  4176. void TextEdit::set_line(int line, String new_text) {
  4177. if (line < 0 || line > text.size())
  4178. return;
  4179. _remove_text(line, 0, line, text[line].length());
  4180. _insert_text(line, 0, new_text);
  4181. if (cursor.line == line) {
  4182. cursor.column = MIN(cursor.column, new_text.length());
  4183. }
  4184. }
  4185. void TextEdit::insert_at(const String &p_text, int at) {
  4186. cursor_set_column(0);
  4187. cursor_set_line(at, false, true);
  4188. _insert_text(at, 0, p_text + "\n");
  4189. }
  4190. void TextEdit::set_show_line_numbers(bool p_show) {
  4191. line_numbers = p_show;
  4192. update();
  4193. }
  4194. void TextEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  4195. line_numbers_zero_padded = p_zero_padded;
  4196. update();
  4197. }
  4198. bool TextEdit::is_show_line_numbers_enabled() const {
  4199. return line_numbers;
  4200. }
  4201. void TextEdit::set_show_line_length_guideline(bool p_show) {
  4202. line_length_guideline = p_show;
  4203. update();
  4204. }
  4205. void TextEdit::set_line_length_guideline_column(int p_column) {
  4206. line_length_guideline_col = p_column;
  4207. update();
  4208. }
  4209. void TextEdit::set_draw_breakpoint_gutter(bool p_draw) {
  4210. draw_breakpoint_gutter = p_draw;
  4211. update();
  4212. }
  4213. bool TextEdit::is_drawing_breakpoint_gutter() const {
  4214. return draw_breakpoint_gutter;
  4215. }
  4216. void TextEdit::set_breakpoint_gutter_width(int p_gutter_width) {
  4217. breakpoint_gutter_width = p_gutter_width;
  4218. update();
  4219. }
  4220. int TextEdit::get_breakpoint_gutter_width() const {
  4221. return cache.breakpoint_gutter_width;
  4222. }
  4223. void TextEdit::set_draw_fold_gutter(bool p_draw) {
  4224. draw_fold_gutter = p_draw;
  4225. update();
  4226. }
  4227. bool TextEdit::is_drawing_fold_gutter() const {
  4228. return draw_fold_gutter;
  4229. }
  4230. void TextEdit::set_fold_gutter_width(int p_gutter_width) {
  4231. fold_gutter_width = p_gutter_width;
  4232. update();
  4233. }
  4234. int TextEdit::get_fold_gutter_width() const {
  4235. return cache.fold_gutter_width;
  4236. }
  4237. void TextEdit::set_hiding_enabled(int p_enabled) {
  4238. if (!p_enabled)
  4239. unhide_all_lines();
  4240. hiding_enabled = p_enabled;
  4241. update();
  4242. }
  4243. int TextEdit::is_hiding_enabled() const {
  4244. return hiding_enabled;
  4245. }
  4246. void TextEdit::set_highlight_current_line(bool p_enabled) {
  4247. highlight_current_line = p_enabled;
  4248. update();
  4249. }
  4250. bool TextEdit::is_highlight_current_line_enabled() const {
  4251. return highlight_current_line;
  4252. }
  4253. bool TextEdit::is_text_field() const {
  4254. return true;
  4255. }
  4256. void TextEdit::menu_option(int p_option) {
  4257. switch (p_option) {
  4258. case MENU_CUT: {
  4259. if (!readonly) {
  4260. cut();
  4261. }
  4262. } break;
  4263. case MENU_COPY: {
  4264. copy();
  4265. } break;
  4266. case MENU_PASTE: {
  4267. if (!readonly) {
  4268. paste();
  4269. }
  4270. } break;
  4271. case MENU_CLEAR: {
  4272. if (!readonly) {
  4273. clear();
  4274. }
  4275. } break;
  4276. case MENU_SELECT_ALL: {
  4277. select_all();
  4278. } break;
  4279. case MENU_UNDO: {
  4280. undo();
  4281. } break;
  4282. };
  4283. }
  4284. void TextEdit::set_select_identifiers_on_hover(bool p_enable) {
  4285. select_identifiers_enabled = p_enable;
  4286. }
  4287. bool TextEdit::is_selecting_identifiers_on_hover_enabled() const {
  4288. return select_identifiers_enabled;
  4289. }
  4290. void TextEdit::set_context_menu_enabled(bool p_enable) {
  4291. context_menu_enabled = p_enable;
  4292. }
  4293. bool TextEdit::is_context_menu_enabled() {
  4294. return context_menu_enabled;
  4295. }
  4296. PopupMenu *TextEdit::get_menu() const {
  4297. return menu;
  4298. }
  4299. void TextEdit::_bind_methods() {
  4300. ClassDB::bind_method(D_METHOD("_gui_input"), &TextEdit::_gui_input);
  4301. ClassDB::bind_method(D_METHOD("_scroll_moved"), &TextEdit::_scroll_moved);
  4302. ClassDB::bind_method(D_METHOD("_cursor_changed_emit"), &TextEdit::_cursor_changed_emit);
  4303. ClassDB::bind_method(D_METHOD("_text_changed_emit"), &TextEdit::_text_changed_emit);
  4304. ClassDB::bind_method(D_METHOD("_push_current_op"), &TextEdit::_push_current_op);
  4305. ClassDB::bind_method(D_METHOD("_click_selection_held"), &TextEdit::_click_selection_held);
  4306. ClassDB::bind_method(D_METHOD("_toggle_draw_caret"), &TextEdit::_toggle_draw_caret);
  4307. ClassDB::bind_method(D_METHOD("_v_scroll_input"), &TextEdit::_v_scroll_input);
  4308. BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE);
  4309. BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS);
  4310. BIND_ENUM_CONSTANT(SEARCH_BACKWARDS);
  4311. /*
  4312. ClassDB::bind_method(D_METHOD("delete_char"),&TextEdit::delete_char);
  4313. ClassDB::bind_method(D_METHOD("delete_line"),&TextEdit::delete_line);
  4314. */
  4315. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text);
  4316. ClassDB::bind_method(D_METHOD("insert_text_at_cursor", "text"), &TextEdit::insert_text_at_cursor);
  4317. ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count);
  4318. ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
  4319. ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
  4320. ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(true));
  4321. ClassDB::bind_method(D_METHOD("cursor_set_line", "line", "adjust_viewport", "can_be_hidden"), &TextEdit::cursor_set_line, DEFVAL(true), DEFVAL(true));
  4322. ClassDB::bind_method(D_METHOD("cursor_get_column"), &TextEdit::cursor_get_column);
  4323. ClassDB::bind_method(D_METHOD("cursor_get_line"), &TextEdit::cursor_get_line);
  4324. ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enable"), &TextEdit::cursor_set_blink_enabled);
  4325. ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &TextEdit::cursor_get_blink_enabled);
  4326. ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &TextEdit::cursor_set_blink_speed);
  4327. ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &TextEdit::cursor_get_blink_speed);
  4328. ClassDB::bind_method(D_METHOD("cursor_set_block_mode", "enable"), &TextEdit::cursor_set_block_mode);
  4329. ClassDB::bind_method(D_METHOD("cursor_is_block_mode"), &TextEdit::cursor_is_block_mode);
  4330. ClassDB::bind_method(D_METHOD("set_readonly", "enable"), &TextEdit::set_readonly);
  4331. ClassDB::bind_method(D_METHOD("is_readonly"), &TextEdit::is_readonly);
  4332. ClassDB::bind_method(D_METHOD("set_wrap", "enable"), &TextEdit::set_wrap);
  4333. ClassDB::bind_method(D_METHOD("set_max_chars", "amount"), &TextEdit::set_max_chars);
  4334. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &TextEdit::set_context_menu_enabled);
  4335. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled);
  4336. ClassDB::bind_method(D_METHOD("cut"), &TextEdit::cut);
  4337. ClassDB::bind_method(D_METHOD("copy"), &TextEdit::copy);
  4338. ClassDB::bind_method(D_METHOD("paste"), &TextEdit::paste);
  4339. ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column"), &TextEdit::select);
  4340. ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
  4341. ClassDB::bind_method(D_METHOD("deselect"), &TextEdit::deselect);
  4342. ClassDB::bind_method(D_METHOD("is_selection_active"), &TextEdit::is_selection_active);
  4343. ClassDB::bind_method(D_METHOD("get_selection_from_line"), &TextEdit::get_selection_from_line);
  4344. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &TextEdit::get_selection_from_column);
  4345. ClassDB::bind_method(D_METHOD("get_selection_to_line"), &TextEdit::get_selection_to_line);
  4346. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &TextEdit::get_selection_to_column);
  4347. ClassDB::bind_method(D_METHOD("get_selection_text"), &TextEdit::get_selection_text);
  4348. ClassDB::bind_method(D_METHOD("get_word_under_cursor"), &TextEdit::get_word_under_cursor);
  4349. ClassDB::bind_method(D_METHOD("search", "key", "flags", "from_line", "from_column"), &TextEdit::_search_bind);
  4350. ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo);
  4351. ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo);
  4352. ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history);
  4353. ClassDB::bind_method(D_METHOD("set_show_line_numbers", "enable"), &TextEdit::set_show_line_numbers);
  4354. ClassDB::bind_method(D_METHOD("is_show_line_numbers_enabled"), &TextEdit::is_show_line_numbers_enabled);
  4355. ClassDB::bind_method(D_METHOD("set_hiding_enabled", "enable"), &TextEdit::set_hiding_enabled);
  4356. ClassDB::bind_method(D_METHOD("is_hiding_enabled"), &TextEdit::is_hiding_enabled);
  4357. ClassDB::bind_method(D_METHOD("set_line_as_hidden", "line", "enable"), &TextEdit::set_line_as_hidden);
  4358. ClassDB::bind_method(D_METHOD("is_line_hidden"), &TextEdit::is_line_hidden);
  4359. ClassDB::bind_method(D_METHOD("fold_all_lines"), &TextEdit::fold_all_lines);
  4360. ClassDB::bind_method(D_METHOD("unhide_all_lines"), &TextEdit::unhide_all_lines);
  4361. ClassDB::bind_method(D_METHOD("fold_line", "line"), &TextEdit::fold_line);
  4362. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &TextEdit::unfold_line);
  4363. ClassDB::bind_method(D_METHOD("toggle_fold_line", "line"), &TextEdit::toggle_fold_line);
  4364. ClassDB::bind_method(D_METHOD("can_fold", "line"), &TextEdit::can_fold);
  4365. ClassDB::bind_method(D_METHOD("is_folded", "line"), &TextEdit::is_folded);
  4366. ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enable"), &TextEdit::set_highlight_all_occurrences);
  4367. ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  4368. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &TextEdit::set_override_selected_font_color);
  4369. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &TextEdit::is_overriding_selected_font_color);
  4370. ClassDB::bind_method(D_METHOD("set_syntax_coloring", "enable"), &TextEdit::set_syntax_coloring);
  4371. ClassDB::bind_method(D_METHOD("is_syntax_coloring_enabled"), &TextEdit::is_syntax_coloring_enabled);
  4372. ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
  4373. ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
  4374. ClassDB::bind_method(D_METHOD("set_smooth_scroll_enable", "enable"), &TextEdit::set_smooth_scroll_enabled);
  4375. ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
  4376. ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
  4377. ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
  4378. ClassDB::bind_method(D_METHOD("add_keyword_color", "keyword", "color"), &TextEdit::add_keyword_color);
  4379. ClassDB::bind_method(D_METHOD("add_color_region", "begin_key", "end_key", "color", "line_only"), &TextEdit::add_color_region, DEFVAL(false));
  4380. ClassDB::bind_method(D_METHOD("clear_colors"), &TextEdit::clear_colors);
  4381. ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
  4382. ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu);
  4383. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  4384. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "readonly"), "set_readonly", "is_readonly");
  4385. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
  4386. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), "set_syntax_coloring", "is_syntax_coloring_enabled");
  4387. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), "set_show_line_numbers", "is_show_line_numbers_enabled");
  4388. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled");
  4389. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  4390. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  4391. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_scrolling"), "set_smooth_scroll_enable", "is_smooth_scroll_enabled");
  4392. ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_scroll_speed"), "set_v_scroll_speed", "get_v_scroll_speed");
  4393. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hiding_enabled"), "set_hiding_enabled", "is_hiding_enabled");
  4394. ADD_GROUP("Caret", "caret_");
  4395. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_block_mode"), "cursor_set_block_mode", "cursor_is_block_mode");
  4396. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  4397. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.1"), "cursor_set_blink_speed", "cursor_get_blink_speed");
  4398. ADD_SIGNAL(MethodInfo("cursor_changed"));
  4399. ADD_SIGNAL(MethodInfo("text_changed"));
  4400. ADD_SIGNAL(MethodInfo("request_completion"));
  4401. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "row")));
  4402. ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "row"), PropertyInfo(Variant::INT, "column")));
  4403. BIND_ENUM_CONSTANT(MENU_CUT);
  4404. BIND_ENUM_CONSTANT(MENU_COPY);
  4405. BIND_ENUM_CONSTANT(MENU_PASTE);
  4406. BIND_ENUM_CONSTANT(MENU_CLEAR);
  4407. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  4408. BIND_ENUM_CONSTANT(MENU_UNDO);
  4409. BIND_ENUM_CONSTANT(MENU_MAX);
  4410. GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3);
  4411. }
  4412. TextEdit::TextEdit() {
  4413. readonly = false;
  4414. setting_row = false;
  4415. draw_tabs = false;
  4416. override_selected_font_color = false;
  4417. draw_caret = true;
  4418. max_chars = 0;
  4419. clear();
  4420. wrap = false;
  4421. set_focus_mode(FOCUS_ALL);
  4422. _update_caches();
  4423. cache.size = Size2(1, 1);
  4424. cache.row_height = 1;
  4425. cache.line_spacing = 1;
  4426. cache.line_number_w = 1;
  4427. cache.breakpoint_gutter_width = 0;
  4428. breakpoint_gutter_width = 0;
  4429. cache.fold_gutter_width = 0;
  4430. fold_gutter_width = 0;
  4431. indent_size = 4;
  4432. text.set_indent_size(indent_size);
  4433. text.clear();
  4434. //text.insert(1,"Mongolia..");
  4435. //text.insert(2,"PAIS GENEROSO!!");
  4436. text.set_color_regions(&color_regions);
  4437. h_scroll = memnew(HScrollBar);
  4438. v_scroll = memnew(VScrollBar);
  4439. add_child(h_scroll);
  4440. add_child(v_scroll);
  4441. updating_scrolls = false;
  4442. selection.active = false;
  4443. h_scroll->connect("value_changed", this, "_scroll_moved");
  4444. v_scroll->connect("value_changed", this, "_scroll_moved");
  4445. v_scroll->connect("scrolling", this, "_v_scroll_input");
  4446. cursor_changed_dirty = false;
  4447. text_changed_dirty = false;
  4448. selection.selecting_mode = Selection::MODE_NONE;
  4449. selection.selecting_line = 0;
  4450. selection.selecting_column = 0;
  4451. selection.selecting_text = false;
  4452. selection.active = false;
  4453. syntax_coloring = false;
  4454. block_caret = false;
  4455. caret_blink_enabled = false;
  4456. caret_blink_timer = memnew(Timer);
  4457. add_child(caret_blink_timer);
  4458. caret_blink_timer->set_wait_time(0.65);
  4459. caret_blink_timer->connect("timeout", this, "_toggle_draw_caret");
  4460. cursor_set_blink_enabled(false);
  4461. idle_detect = memnew(Timer);
  4462. add_child(idle_detect);
  4463. idle_detect->set_one_shot(true);
  4464. idle_detect->set_wait_time(GLOBAL_GET("gui/timers/text_edit_idle_detect_sec"));
  4465. idle_detect->connect("timeout", this, "_push_current_op");
  4466. click_select_held = memnew(Timer);
  4467. add_child(click_select_held);
  4468. click_select_held->set_wait_time(0.05);
  4469. click_select_held->connect("timeout", this, "_click_selection_held");
  4470. current_op.type = TextOperation::TYPE_NONE;
  4471. undo_enabled = true;
  4472. undo_stack_pos = NULL;
  4473. setting_text = false;
  4474. last_dblclk = 0;
  4475. current_op.version = 0;
  4476. version = 0;
  4477. saved_version = 0;
  4478. completion_enabled = false;
  4479. completion_active = false;
  4480. completion_line_ofs = 0;
  4481. tooltip_obj = NULL;
  4482. line_numbers = false;
  4483. line_numbers_zero_padded = false;
  4484. line_length_guideline = false;
  4485. line_length_guideline_col = 80;
  4486. draw_breakpoint_gutter = false;
  4487. draw_fold_gutter = false;
  4488. hiding_enabled = false;
  4489. next_operation_is_complex = false;
  4490. scroll_past_end_of_file_enabled = false;
  4491. auto_brace_completion_enabled = false;
  4492. brace_matching_enabled = false;
  4493. highlight_all_occurrences = false;
  4494. highlight_current_line = false;
  4495. indent_using_spaces = false;
  4496. space_indent = " ";
  4497. auto_indent = false;
  4498. insert_mode = false;
  4499. window_has_focus = true;
  4500. select_identifiers_enabled = false;
  4501. smooth_scroll_enabled = false;
  4502. scrolling = false;
  4503. target_v_scroll = 0;
  4504. v_scroll_speed = 80;
  4505. raised_from_completion = false;
  4506. context_menu_enabled = true;
  4507. menu = memnew(PopupMenu);
  4508. add_child(menu);
  4509. menu->add_item(TTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X);
  4510. menu->add_item(TTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C);
  4511. menu->add_item(TTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V);
  4512. menu->add_separator();
  4513. menu->add_item(TTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A);
  4514. menu->add_item(TTR("Clear"), MENU_CLEAR);
  4515. menu->add_separator();
  4516. menu->add_item(TTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z);
  4517. menu->connect("id_pressed", this, "menu_option");
  4518. }
  4519. TextEdit::~TextEdit() {
  4520. }