text_edit.cpp 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724
  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. _reset_caret_blink_timer();
  1579. int row, col;
  1580. update_line_scroll_pos();
  1581. _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
  1582. if (is_right_click_moving_caret()) {
  1583. if (is_selection_active()) {
  1584. int from_line = get_selection_from_line();
  1585. int to_line = get_selection_to_line();
  1586. int from_column = get_selection_from_column();
  1587. int to_column = get_selection_to_column();
  1588. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  1589. // Right click is outside the seleted text
  1590. deselect();
  1591. }
  1592. }
  1593. if (!is_selection_active()) {
  1594. cursor_set_line(row, true, false);
  1595. cursor_set_column(col);
  1596. }
  1597. }
  1598. menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  1599. menu->set_size(Vector2(1, 1));
  1600. menu->popup();
  1601. grab_focus();
  1602. }
  1603. } else {
  1604. if (mb->get_button_index() == BUTTON_LEFT)
  1605. click_select_held->stop();
  1606. // notify to show soft keyboard
  1607. notification(NOTIFICATION_FOCUS_ENTER);
  1608. }
  1609. }
  1610. const Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  1611. if (pan_gesture.is_valid()) {
  1612. const real_t delta = pan_gesture->get_delta().y;
  1613. if (delta < 0) {
  1614. _scroll_up(-delta);
  1615. } else {
  1616. _scroll_down(delta);
  1617. }
  1618. h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
  1619. return;
  1620. }
  1621. Ref<InputEventMouseMotion> mm = p_gui_input;
  1622. if (mm.is_valid()) {
  1623. if (select_identifiers_enabled) {
  1624. if (mm->get_command() && mm->get_button_mask() == 0) {
  1625. String new_word = get_word_at_pos(mm->get_position());
  1626. if (new_word != highlighted_word) {
  1627. highlighted_word = new_word;
  1628. update();
  1629. }
  1630. } else {
  1631. if (highlighted_word != String()) {
  1632. highlighted_word = String();
  1633. update();
  1634. }
  1635. }
  1636. }
  1637. if (mm->get_button_mask() & BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { //ignore if dragging
  1638. _reset_caret_blink_timer();
  1639. switch (selection.selecting_mode) {
  1640. case Selection::MODE_POINTER: {
  1641. _update_selection_mode_pointer();
  1642. } break;
  1643. case Selection::MODE_WORD: {
  1644. _update_selection_mode_word();
  1645. } break;
  1646. case Selection::MODE_LINE: {
  1647. _update_selection_mode_line();
  1648. } break;
  1649. default: {
  1650. break;
  1651. }
  1652. }
  1653. }
  1654. }
  1655. Ref<InputEventKey> k = p_gui_input;
  1656. if (k.is_valid()) {
  1657. k = k->duplicate(); //it will be modified later on
  1658. #ifdef OSX_ENABLED
  1659. if (k->get_scancode() == KEY_META) {
  1660. #else
  1661. if (k->get_scancode() == KEY_CONTROL) {
  1662. #endif
  1663. if (select_identifiers_enabled) {
  1664. if (k->is_pressed()) {
  1665. highlighted_word = get_word_at_pos(get_local_mouse_position());
  1666. update();
  1667. } else {
  1668. highlighted_word = String();
  1669. update();
  1670. }
  1671. }
  1672. }
  1673. if (!k->is_pressed())
  1674. return;
  1675. if (completion_active) {
  1676. if (readonly)
  1677. return;
  1678. bool valid = true;
  1679. if (k->get_command() || k->get_metakey())
  1680. valid = false;
  1681. if (valid) {
  1682. if (!k->get_alt()) {
  1683. if (k->get_scancode() == KEY_UP) {
  1684. if (completion_index > 0) {
  1685. completion_index--;
  1686. completion_current = completion_options[completion_index];
  1687. update();
  1688. }
  1689. accept_event();
  1690. return;
  1691. }
  1692. if (k->get_scancode() == KEY_DOWN) {
  1693. if (completion_index < completion_options.size() - 1) {
  1694. completion_index++;
  1695. completion_current = completion_options[completion_index];
  1696. update();
  1697. }
  1698. accept_event();
  1699. return;
  1700. }
  1701. if (k->get_scancode() == KEY_PAGEUP) {
  1702. completion_index -= get_constant("completion_lines");
  1703. if (completion_index < 0)
  1704. completion_index = 0;
  1705. completion_current = completion_options[completion_index];
  1706. update();
  1707. accept_event();
  1708. return;
  1709. }
  1710. if (k->get_scancode() == KEY_PAGEDOWN) {
  1711. completion_index += get_constant("completion_lines");
  1712. if (completion_index >= completion_options.size())
  1713. completion_index = completion_options.size() - 1;
  1714. completion_current = completion_options[completion_index];
  1715. update();
  1716. accept_event();
  1717. return;
  1718. }
  1719. if (k->get_scancode() == KEY_HOME && completion_index > 0) {
  1720. completion_index = 0;
  1721. completion_current = completion_options[completion_index];
  1722. update();
  1723. accept_event();
  1724. return;
  1725. }
  1726. if (k->get_scancode() == KEY_END && completion_index < completion_options.size() - 1) {
  1727. completion_index = completion_options.size() - 1;
  1728. completion_current = completion_options[completion_index];
  1729. update();
  1730. accept_event();
  1731. return;
  1732. }
  1733. if (k->get_scancode() == KEY_KP_ENTER || k->get_scancode() == KEY_ENTER || k->get_scancode() == KEY_TAB) {
  1734. _confirm_completion();
  1735. accept_event();
  1736. return;
  1737. }
  1738. if (k->get_scancode() == KEY_BACKSPACE) {
  1739. _reset_caret_blink_timer();
  1740. backspace_at_cursor();
  1741. _update_completion_candidates();
  1742. accept_event();
  1743. return;
  1744. }
  1745. if (k->get_scancode() == KEY_SHIFT) {
  1746. accept_event();
  1747. return;
  1748. }
  1749. }
  1750. if (k->get_unicode() > 32) {
  1751. _reset_caret_blink_timer();
  1752. const CharType chr[2] = { (CharType)k->get_unicode(), 0 };
  1753. if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  1754. _consume_pair_symbol(chr[0]);
  1755. } else {
  1756. // remove the old character if in insert mode
  1757. if (insert_mode) {
  1758. begin_complex_operation();
  1759. // make sure we don't try and remove empty space
  1760. if (cursor.column < get_line(cursor.line).length()) {
  1761. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  1762. }
  1763. }
  1764. _insert_text_at_cursor(chr);
  1765. if (insert_mode) {
  1766. end_complex_operation();
  1767. }
  1768. }
  1769. _update_completion_candidates();
  1770. accept_event();
  1771. return;
  1772. }
  1773. }
  1774. _cancel_completion();
  1775. }
  1776. /* TEST CONTROL FIRST!! */
  1777. // some remaps for duplicate functions..
  1778. if (k->get_command() && !k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_scancode() == KEY_INSERT) {
  1779. k->set_scancode(KEY_C);
  1780. }
  1781. if (!k->get_command() && k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_scancode() == KEY_INSERT) {
  1782. k->set_scancode(KEY_V);
  1783. k->set_command(true);
  1784. k->set_shift(false);
  1785. }
  1786. if (!k->get_command()) {
  1787. _reset_caret_blink_timer();
  1788. }
  1789. // save here for insert mode, just in case it is cleared in the following section
  1790. bool had_selection = selection.active;
  1791. // stuff to do when selection is active..
  1792. if (selection.active) {
  1793. if (readonly)
  1794. return;
  1795. bool clear = false;
  1796. bool unselect = false;
  1797. bool dobreak = false;
  1798. switch (k->get_scancode()) {
  1799. case KEY_TAB: {
  1800. if (k->get_shift()) {
  1801. indent_left();
  1802. } else {
  1803. indent_right();
  1804. }
  1805. dobreak = true;
  1806. accept_event();
  1807. } break;
  1808. case KEY_X:
  1809. case KEY_C:
  1810. //special keys often used with control, wait...
  1811. clear = (!k->get_command() || k->get_shift() || k->get_alt());
  1812. break;
  1813. case KEY_DELETE:
  1814. if (!k->get_shift()) {
  1815. accept_event();
  1816. clear = true;
  1817. dobreak = true;
  1818. } else if (k->get_command() || k->get_alt()) {
  1819. dobreak = true;
  1820. }
  1821. break;
  1822. case KEY_BACKSPACE:
  1823. accept_event();
  1824. clear = true;
  1825. dobreak = true;
  1826. break;
  1827. case KEY_LEFT:
  1828. case KEY_RIGHT:
  1829. case KEY_UP:
  1830. case KEY_DOWN:
  1831. case KEY_PAGEUP:
  1832. case KEY_PAGEDOWN:
  1833. case KEY_HOME:
  1834. case KEY_END:
  1835. // ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys)
  1836. if (k->get_command() || k->get_shift() || k->get_alt())
  1837. break;
  1838. unselect = true;
  1839. break;
  1840. default:
  1841. if (k->get_unicode() >= 32 && !k->get_command() && !k->get_alt() && !k->get_metakey())
  1842. clear = true;
  1843. if (auto_brace_completion_enabled && _is_pair_left_symbol(k->get_unicode()))
  1844. clear = false;
  1845. }
  1846. if (unselect) {
  1847. selection.active = false;
  1848. selection.selecting_mode = Selection::MODE_NONE;
  1849. update();
  1850. }
  1851. if (clear) {
  1852. if (!dobreak) {
  1853. begin_complex_operation();
  1854. }
  1855. selection.active = false;
  1856. update();
  1857. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  1858. cursor_set_line(selection.from_line, true, false);
  1859. cursor_set_column(selection.from_column);
  1860. update();
  1861. }
  1862. if (dobreak)
  1863. return;
  1864. }
  1865. selection.selecting_text = false;
  1866. bool scancode_handled = true;
  1867. // special scancode test...
  1868. switch (k->get_scancode()) {
  1869. case KEY_KP_ENTER:
  1870. case KEY_ENTER: {
  1871. if (readonly)
  1872. break;
  1873. String ins = "\n";
  1874. //keep indentation
  1875. int space_count = 0;
  1876. for (int i = 0; i < cursor.column; i++) {
  1877. if (text[cursor.line][i] == '\t') {
  1878. if (indent_using_spaces) {
  1879. ins += space_indent;
  1880. } else {
  1881. ins += "\t";
  1882. }
  1883. space_count = 0;
  1884. } else if (text[cursor.line][i] == ' ') {
  1885. space_count++;
  1886. if (space_count == indent_size) {
  1887. if (indent_using_spaces) {
  1888. ins += space_indent;
  1889. } else {
  1890. ins += "\t";
  1891. }
  1892. space_count = 0;
  1893. }
  1894. } else {
  1895. break;
  1896. }
  1897. }
  1898. if (is_folded(cursor.line))
  1899. unfold_line(cursor.line);
  1900. bool brace_indent = false;
  1901. // no need to indent if we are going upwards.
  1902. if (auto_indent && !(k->get_command() && k->get_shift())) {
  1903. // indent once again if previous line will end with ':' or '{'
  1904. // (i.e. colon/brace precedes current cursor position)
  1905. if (cursor.column > 0 && (text[cursor.line][cursor.column - 1] == ':' || text[cursor.line][cursor.column - 1] == '{')) {
  1906. if (indent_using_spaces) {
  1907. ins += space_indent;
  1908. } else {
  1909. ins += "\t";
  1910. }
  1911. // no need to move the brace below if we are not taking the text with us.
  1912. if (text[cursor.line][cursor.column] == '}' && !k->get_command()) {
  1913. brace_indent = true;
  1914. ins += "\n" + ins.substr(1, ins.length() - 2);
  1915. }
  1916. }
  1917. }
  1918. begin_complex_operation();
  1919. bool first_line = false;
  1920. if (k->get_command()) {
  1921. if (k->get_shift()) {
  1922. if (cursor.line > 0) {
  1923. cursor_set_line(cursor.line - 1);
  1924. cursor_set_column(text[cursor.line].length());
  1925. } else {
  1926. cursor_set_column(0);
  1927. first_line = true;
  1928. }
  1929. } else {
  1930. cursor_set_column(text[cursor.line].length());
  1931. }
  1932. }
  1933. insert_text_at_cursor(ins);
  1934. if (first_line) {
  1935. cursor_set_line(0);
  1936. } else if (brace_indent) {
  1937. cursor_set_line(cursor.line - 1);
  1938. cursor_set_column(text[cursor.line].length());
  1939. }
  1940. end_complex_operation();
  1941. } break;
  1942. case KEY_ESCAPE: {
  1943. if (completion_hint != "") {
  1944. completion_hint = "";
  1945. update();
  1946. } else {
  1947. scancode_handled = false;
  1948. }
  1949. } break;
  1950. case KEY_TAB: {
  1951. if (k->get_command()) break; // avoid tab when command
  1952. if (readonly)
  1953. break;
  1954. if (is_selection_active()) {
  1955. if (k->get_shift()) {
  1956. indent_left();
  1957. } else {
  1958. indent_right();
  1959. }
  1960. } else {
  1961. if (k->get_shift()) {
  1962. //simple unindent
  1963. int cc = cursor.column;
  1964. if (cc > 0 && cc <= text[cursor.line].length()) {
  1965. if (text[cursor.line][cursor.column - 1] == '\t') {
  1966. backspace_at_cursor();
  1967. } else {
  1968. if (cursor.column - indent_size >= 0) {
  1969. bool unindent = true;
  1970. for (int i = 1; i <= indent_size; i++) {
  1971. if (text[cursor.line][cursor.column - i] != ' ') {
  1972. unindent = false;
  1973. break;
  1974. }
  1975. }
  1976. if (unindent) {
  1977. _remove_text(cursor.line, cursor.column - indent_size, cursor.line, cursor.column);
  1978. cursor_set_column(cursor.column - indent_size);
  1979. }
  1980. }
  1981. }
  1982. }
  1983. } else {
  1984. //simple indent
  1985. if (indent_using_spaces) {
  1986. _insert_text_at_cursor(space_indent);
  1987. } else {
  1988. _insert_text_at_cursor("\t");
  1989. }
  1990. }
  1991. }
  1992. } break;
  1993. case KEY_BACKSPACE: {
  1994. if (readonly)
  1995. break;
  1996. #ifdef APPLE_STYLE_KEYS
  1997. if (k->get_alt() && cursor.column > 1) {
  1998. #else
  1999. if (k->get_alt()) {
  2000. scancode_handled = false;
  2001. break;
  2002. } else if (k->get_command() && cursor.column > 1) {
  2003. #endif
  2004. int line = cursor.line;
  2005. int column = cursor.column;
  2006. // check if we are removing a single whitespace, if so remove it and the next char type
  2007. // else we just remove the whitespace
  2008. bool only_whitespace = false;
  2009. if (_is_whitespace(text[line][column - 1]) && _is_whitespace(text[line][column - 2])) {
  2010. only_whitespace = true;
  2011. } else if (_is_whitespace(text[line][column - 1])) {
  2012. // remove the single whitespace
  2013. column--;
  2014. }
  2015. // check if its a text char
  2016. bool only_char = (_is_text_char(text[line][column - 1]) && !only_whitespace);
  2017. // if its not whitespace or char then symbol.
  2018. bool only_symbols = !(only_whitespace || only_char);
  2019. while (column > 0) {
  2020. bool is_whitespace = _is_whitespace(text[line][column - 1]);
  2021. bool is_text_char = _is_text_char(text[line][column - 1]);
  2022. if (only_whitespace && !is_whitespace) {
  2023. break;
  2024. } else if (only_char && !is_text_char) {
  2025. break;
  2026. } else if (only_symbols && (is_whitespace || is_text_char)) {
  2027. break;
  2028. }
  2029. column--;
  2030. }
  2031. _remove_text(line, column, cursor.line, cursor.column);
  2032. cursor_set_line(line);
  2033. cursor_set_column(column);
  2034. } else {
  2035. if (cursor.line > 0 && is_line_hidden(cursor.line - 1))
  2036. unfold_line(cursor.line - 1);
  2037. backspace_at_cursor();
  2038. }
  2039. } break;
  2040. case KEY_KP_4: {
  2041. if (k->get_unicode() != 0) {
  2042. scancode_handled = false;
  2043. break;
  2044. }
  2045. // numlock disabled. fallthrough to key_left
  2046. }
  2047. case KEY_LEFT: {
  2048. if (k->get_shift())
  2049. _pre_shift_selection();
  2050. #ifdef APPLE_STYLE_KEYS
  2051. else
  2052. #else
  2053. else if (!k->get_alt())
  2054. #endif
  2055. deselect();
  2056. #ifdef APPLE_STYLE_KEYS
  2057. if (k->get_command()) {
  2058. cursor_set_column(0);
  2059. } else if (k->get_alt()) {
  2060. #else
  2061. if (k->get_alt()) {
  2062. scancode_handled = false;
  2063. break;
  2064. } else if (k->get_command()) {
  2065. #endif
  2066. bool prev_char = false;
  2067. int cc = cursor.column;
  2068. if (cc == 0 && cursor.line > 0) {
  2069. cursor_set_line(cursor.line - 1);
  2070. cursor_set_column(text[cursor.line].length());
  2071. break;
  2072. }
  2073. while (cc > 0) {
  2074. bool ischar = _is_text_char(text[cursor.line][cc - 1]);
  2075. if (prev_char && !ischar)
  2076. break;
  2077. prev_char = ischar;
  2078. cc--;
  2079. }
  2080. cursor_set_column(cc);
  2081. } else if (cursor.column == 0) {
  2082. if (cursor.line > 0) {
  2083. cursor_set_line(cursor.line - num_lines_from(CLAMP(cursor.line - 1, 0, text.size() - 1), -1));
  2084. cursor_set_column(text[cursor.line].length());
  2085. }
  2086. } else {
  2087. cursor_set_column(cursor_get_column() - 1);
  2088. }
  2089. if (k->get_shift())
  2090. _post_shift_selection();
  2091. } break;
  2092. case KEY_KP_6: {
  2093. if (k->get_unicode() != 0) {
  2094. scancode_handled = false;
  2095. break;
  2096. }
  2097. // numlock disabled. fallthrough to key_right
  2098. }
  2099. case KEY_RIGHT: {
  2100. if (k->get_shift())
  2101. _pre_shift_selection();
  2102. #ifdef APPLE_STYLE_KEYS
  2103. else
  2104. #else
  2105. else if (!k->get_alt())
  2106. #endif
  2107. deselect();
  2108. #ifdef APPLE_STYLE_KEYS
  2109. if (k->get_command()) {
  2110. cursor_set_column(text[cursor.line].length());
  2111. } else if (k->get_alt()) {
  2112. #else
  2113. if (k->get_alt()) {
  2114. scancode_handled = false;
  2115. break;
  2116. } else if (k->get_command()) {
  2117. #endif
  2118. bool prev_char = false;
  2119. int cc = cursor.column;
  2120. if (cc == text[cursor.line].length() && cursor.line < text.size() - 1) {
  2121. cursor_set_line(cursor.line + 1);
  2122. cursor_set_column(0);
  2123. break;
  2124. }
  2125. while (cc < text[cursor.line].length()) {
  2126. bool ischar = _is_text_char(text[cursor.line][cc]);
  2127. if (prev_char && !ischar)
  2128. break;
  2129. prev_char = ischar;
  2130. cc++;
  2131. }
  2132. cursor_set_column(cc);
  2133. } else if (cursor.column == text[cursor.line].length()) {
  2134. if (cursor.line < text.size() - 1) {
  2135. cursor_set_line(cursor_get_line() + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1), true, false);
  2136. cursor_set_column(0);
  2137. }
  2138. } else {
  2139. cursor_set_column(cursor_get_column() + 1);
  2140. }
  2141. if (k->get_shift())
  2142. _post_shift_selection();
  2143. } break;
  2144. case KEY_KP_8: {
  2145. if (k->get_unicode() != 0) {
  2146. scancode_handled = false;
  2147. break;
  2148. }
  2149. // numlock disabled. fallthrough to key_up
  2150. }
  2151. case KEY_UP: {
  2152. if (k->get_shift())
  2153. _pre_shift_selection();
  2154. if (k->get_alt()) {
  2155. scancode_handled = false;
  2156. break;
  2157. }
  2158. #ifndef APPLE_STYLE_KEYS
  2159. if (k->get_command()) {
  2160. _scroll_lines_up();
  2161. break;
  2162. }
  2163. #else
  2164. if (k->get_command() && k->get_alt()) {
  2165. _scroll_lines_up();
  2166. break;
  2167. }
  2168. if (k->get_command())
  2169. cursor_set_line(0);
  2170. else
  2171. #endif
  2172. cursor_set_line(cursor_get_line() - num_lines_from(CLAMP(cursor.line - 1, 0, text.size() - 1), -1));
  2173. if (k->get_shift())
  2174. _post_shift_selection();
  2175. _cancel_code_hint();
  2176. } break;
  2177. case KEY_KP_2: {
  2178. if (k->get_unicode() != 0) {
  2179. scancode_handled = false;
  2180. break;
  2181. }
  2182. // numlock disabled. fallthrough to key_down
  2183. }
  2184. case KEY_DOWN: {
  2185. if (k->get_shift())
  2186. _pre_shift_selection();
  2187. if (k->get_alt()) {
  2188. scancode_handled = false;
  2189. break;
  2190. }
  2191. #ifndef APPLE_STYLE_KEYS
  2192. if (k->get_command()) {
  2193. _scroll_lines_down();
  2194. break;
  2195. }
  2196. #else
  2197. if (k->get_command() && k->get_alt()) {
  2198. _scroll_lines_down();
  2199. break;
  2200. }
  2201. if (k->get_command())
  2202. cursor_set_line(text.size() - 1, true, false);
  2203. else
  2204. #endif
  2205. cursor_set_line(cursor_get_line() + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1), true, false);
  2206. if (k->get_shift())
  2207. _post_shift_selection();
  2208. _cancel_code_hint();
  2209. } break;
  2210. case KEY_DELETE: {
  2211. if (readonly)
  2212. break;
  2213. if (k->get_shift() && !k->get_command() && !k->get_alt()) {
  2214. cut();
  2215. break;
  2216. }
  2217. int curline_len = text[cursor.line].length();
  2218. if (cursor.line == text.size() - 1 && cursor.column == curline_len)
  2219. break; //nothing to do
  2220. int next_line = cursor.column < curline_len ? cursor.line : cursor.line + 1;
  2221. int next_column;
  2222. #ifdef APPLE_STYLE_KEYS
  2223. if (k->get_alt() && cursor.column < curline_len - 1) {
  2224. #else
  2225. if (k->get_alt()) {
  2226. scancode_handled = false;
  2227. break;
  2228. } else if (k->get_command() && cursor.column < curline_len - 1) {
  2229. #endif
  2230. int line = cursor.line;
  2231. int column = cursor.column;
  2232. // check if we are removing a single whitespace, if so remove it and the next char type
  2233. // else we just remove the whitespace
  2234. bool only_whitespace = false;
  2235. if (_is_whitespace(text[line][column]) && _is_whitespace(text[line][column + 1])) {
  2236. only_whitespace = true;
  2237. } else if (_is_whitespace(text[line][column])) {
  2238. // remove the single whitespace
  2239. column++;
  2240. }
  2241. // check if its a text char
  2242. bool only_char = (_is_text_char(text[line][column]) && !only_whitespace);
  2243. // if its not whitespace or char then symbol.
  2244. bool only_symbols = !(only_whitespace || only_char);
  2245. while (column < curline_len) {
  2246. bool is_whitespace = _is_whitespace(text[line][column]);
  2247. bool is_text_char = _is_text_char(text[line][column]);
  2248. if (only_whitespace && !is_whitespace) {
  2249. break;
  2250. } else if (only_char && !is_text_char) {
  2251. break;
  2252. } else if (only_symbols && (is_whitespace || is_text_char)) {
  2253. break;
  2254. }
  2255. column++;
  2256. }
  2257. next_line = line;
  2258. next_column = column;
  2259. } else {
  2260. next_column = cursor.column < curline_len ? (cursor.column + 1) : 0;
  2261. }
  2262. _remove_text(cursor.line, cursor.column, next_line, next_column);
  2263. update();
  2264. } break;
  2265. case KEY_KP_7: {
  2266. if (k->get_unicode() != 0) {
  2267. scancode_handled = false;
  2268. break;
  2269. }
  2270. // numlock disabled. fallthrough to key_home
  2271. }
  2272. #ifdef APPLE_STYLE_KEYS
  2273. case KEY_HOME: {
  2274. if (k->get_shift())
  2275. _pre_shift_selection();
  2276. cursor_set_line(0);
  2277. if (k->get_shift())
  2278. _post_shift_selection();
  2279. else if (k->get_command() || k->get_control())
  2280. deselect();
  2281. } break;
  2282. #else
  2283. case KEY_HOME: {
  2284. if (k->get_shift())
  2285. _pre_shift_selection();
  2286. if (k->get_command()) {
  2287. cursor_set_line(0);
  2288. cursor_set_column(0);
  2289. } else {
  2290. // compute whitespace symbols seq length
  2291. int current_line_whitespace_len = 0;
  2292. while (current_line_whitespace_len < text[cursor.line].length()) {
  2293. CharType c = text[cursor.line][current_line_whitespace_len];
  2294. if (c != '\t' && c != ' ')
  2295. break;
  2296. current_line_whitespace_len++;
  2297. }
  2298. if (cursor_get_column() == current_line_whitespace_len)
  2299. cursor_set_column(0);
  2300. else
  2301. cursor_set_column(current_line_whitespace_len);
  2302. }
  2303. if (k->get_shift())
  2304. _post_shift_selection();
  2305. else if (k->get_command() || k->get_control())
  2306. deselect();
  2307. _cancel_completion();
  2308. completion_hint = "";
  2309. } break;
  2310. #endif
  2311. case KEY_KP_1: {
  2312. if (k->get_unicode() != 0) {
  2313. scancode_handled = false;
  2314. break;
  2315. }
  2316. // numlock disabled. fallthrough to key_end
  2317. }
  2318. #ifdef APPLE_STYLE_KEYS
  2319. case KEY_END: {
  2320. if (k->get_shift())
  2321. _pre_shift_selection();
  2322. cursor_set_line(text.size() - 1, true, false);
  2323. if (k->get_shift())
  2324. _post_shift_selection();
  2325. else if (k->get_command() || k->get_control())
  2326. deselect();
  2327. } break;
  2328. #else
  2329. case KEY_END: {
  2330. if (k->get_shift())
  2331. _pre_shift_selection();
  2332. if (k->get_command())
  2333. cursor_set_line(text.size() - 1, true, false);
  2334. cursor_set_column(text[cursor.line].length());
  2335. if (k->get_shift())
  2336. _post_shift_selection();
  2337. else if (k->get_command() || k->get_control())
  2338. deselect();
  2339. _cancel_completion();
  2340. completion_hint = "";
  2341. } break;
  2342. #endif
  2343. case KEY_KP_9: {
  2344. if (k->get_unicode() != 0) {
  2345. scancode_handled = false;
  2346. break;
  2347. }
  2348. // numlock disabled. fallthrough to key_pageup
  2349. }
  2350. case KEY_PAGEUP: {
  2351. if (k->get_shift())
  2352. _pre_shift_selection();
  2353. cursor_set_line(cursor_get_line() - num_lines_from(cursor.line, -get_visible_rows()), true, false);
  2354. if (k->get_shift())
  2355. _post_shift_selection();
  2356. _cancel_completion();
  2357. completion_hint = "";
  2358. } break;
  2359. case KEY_KP_3: {
  2360. if (k->get_unicode() != 0) {
  2361. scancode_handled = false;
  2362. break;
  2363. }
  2364. // numlock disabled. fallthrough to key_pageup
  2365. }
  2366. case KEY_PAGEDOWN: {
  2367. if (k->get_shift())
  2368. _pre_shift_selection();
  2369. cursor_set_line(cursor_get_line() + num_lines_from(cursor.line, get_visible_rows()), true, false);
  2370. if (k->get_shift())
  2371. _post_shift_selection();
  2372. _cancel_completion();
  2373. completion_hint = "";
  2374. } break;
  2375. case KEY_A: {
  2376. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2377. scancode_handled = false;
  2378. break;
  2379. }
  2380. select_all();
  2381. } break;
  2382. case KEY_X: {
  2383. if (readonly) {
  2384. break;
  2385. }
  2386. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2387. scancode_handled = false;
  2388. break;
  2389. }
  2390. cut();
  2391. } break;
  2392. case KEY_C: {
  2393. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2394. scancode_handled = false;
  2395. break;
  2396. }
  2397. copy();
  2398. } break;
  2399. case KEY_Z: {
  2400. if (!k->get_command()) {
  2401. scancode_handled = false;
  2402. break;
  2403. }
  2404. if (k->get_shift())
  2405. redo();
  2406. else
  2407. undo();
  2408. } break;
  2409. case KEY_Y: {
  2410. if (!k->get_command()) {
  2411. scancode_handled = false;
  2412. break;
  2413. }
  2414. redo();
  2415. } break;
  2416. case KEY_V: {
  2417. if (readonly) {
  2418. break;
  2419. }
  2420. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2421. scancode_handled = false;
  2422. break;
  2423. }
  2424. paste();
  2425. } break;
  2426. case KEY_SPACE: {
  2427. #ifdef OSX_ENABLED
  2428. if (completion_enabled && k->get_metakey()) { //cmd-space is spotlight shortcut in OSX
  2429. #else
  2430. if (completion_enabled && k->get_command()) {
  2431. #endif
  2432. query_code_comple();
  2433. scancode_handled = true;
  2434. } else {
  2435. scancode_handled = false;
  2436. }
  2437. } break;
  2438. case KEY_U: {
  2439. if (!k->get_command() || k->get_shift()) {
  2440. scancode_handled = false;
  2441. break;
  2442. } else {
  2443. if (selection.active) {
  2444. int ini = selection.from_line;
  2445. int end = selection.to_line;
  2446. for (int i = ini; i <= end; i++) {
  2447. if (get_line(i).begins_with("#"))
  2448. _remove_text(i, 0, i, 1);
  2449. }
  2450. } else {
  2451. if (get_line(cursor.line).begins_with("#")) {
  2452. _remove_text(cursor.line, 0, cursor.line, 1);
  2453. if (cursor.column >= get_line(cursor.line).length()) {
  2454. cursor.column = MAX(0, get_line(cursor.line).length() - 1);
  2455. }
  2456. }
  2457. }
  2458. update();
  2459. }
  2460. } break;
  2461. default: {
  2462. scancode_handled = false;
  2463. } break;
  2464. }
  2465. if (scancode_handled)
  2466. accept_event();
  2467. /*
  2468. if (!scancode_handled && !k->get_command() && !k->get_alt()) {
  2469. if (k->get_unicode()>=32) {
  2470. if (readonly)
  2471. break;
  2472. accept_event();
  2473. } else {
  2474. break;
  2475. }
  2476. }
  2477. */
  2478. if (k->get_scancode() == KEY_INSERT) {
  2479. set_insert_mode(!insert_mode);
  2480. accept_event();
  2481. return;
  2482. }
  2483. if (!scancode_handled && !k->get_command()) { //for german kbds
  2484. if (k->get_unicode() >= 32) {
  2485. if (readonly)
  2486. return;
  2487. // remove the old character if in insert mode and no selection
  2488. if (insert_mode && !had_selection) {
  2489. begin_complex_operation();
  2490. // make sure we don't try and remove empty space
  2491. if (cursor.column < get_line(cursor.line).length()) {
  2492. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  2493. }
  2494. }
  2495. const CharType chr[2] = { (CharType)k->get_unicode(), 0 };
  2496. if (completion_hint != "" && k->get_unicode() == ')') {
  2497. completion_hint = "";
  2498. }
  2499. if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  2500. _consume_pair_symbol(chr[0]);
  2501. } else {
  2502. _insert_text_at_cursor(chr);
  2503. }
  2504. if (insert_mode && !had_selection) {
  2505. end_complex_operation();
  2506. }
  2507. if (selection.active != had_selection) {
  2508. end_complex_operation();
  2509. }
  2510. accept_event();
  2511. } else {
  2512. }
  2513. }
  2514. return;
  2515. }
  2516. }
  2517. void TextEdit::_scroll_up(real_t p_delta) {
  2518. if (scrolling) {
  2519. target_v_scroll = (target_v_scroll - p_delta);
  2520. } else {
  2521. target_v_scroll = (v_scroll->get_value() - p_delta);
  2522. }
  2523. if (smooth_scroll_enabled) {
  2524. if (target_v_scroll <= 0) {
  2525. target_v_scroll = 0;
  2526. }
  2527. scrolling = true;
  2528. set_physics_process(true);
  2529. } else {
  2530. v_scroll->set_value(target_v_scroll);
  2531. }
  2532. }
  2533. void TextEdit::_scroll_down(real_t p_delta) {
  2534. if (scrolling) {
  2535. target_v_scroll = (target_v_scroll + p_delta);
  2536. } else {
  2537. target_v_scroll = (v_scroll->get_value() + p_delta);
  2538. }
  2539. if (smooth_scroll_enabled) {
  2540. int max_v_scroll = get_total_unhidden_rows();
  2541. if (!scroll_past_end_of_file_enabled) {
  2542. max_v_scroll -= get_visible_rows();
  2543. max_v_scroll = CLAMP(max_v_scroll, 0, get_total_unhidden_rows());
  2544. }
  2545. if (target_v_scroll > max_v_scroll) {
  2546. target_v_scroll = max_v_scroll;
  2547. }
  2548. scrolling = true;
  2549. set_physics_process(true);
  2550. } else {
  2551. v_scroll->set_value(target_v_scroll);
  2552. }
  2553. }
  2554. void TextEdit::_pre_shift_selection() {
  2555. if (!selection.active || selection.selecting_mode == Selection::MODE_NONE) {
  2556. selection.selecting_line = cursor.line;
  2557. selection.selecting_column = cursor.column;
  2558. selection.active = true;
  2559. }
  2560. selection.selecting_mode = Selection::MODE_SHIFT;
  2561. }
  2562. void TextEdit::_post_shift_selection() {
  2563. if (selection.active && selection.selecting_mode == Selection::MODE_SHIFT) {
  2564. select(selection.selecting_line, selection.selecting_column, cursor.line, cursor.column);
  2565. update();
  2566. }
  2567. selection.selecting_text = true;
  2568. }
  2569. void TextEdit::_scroll_lines_up() {
  2570. scrolling = false;
  2571. // adjust the vertical scroll
  2572. if (get_v_scroll() >= 0) {
  2573. set_v_scroll(get_v_scroll() - 1);
  2574. }
  2575. // adjust the cursor
  2576. int num_lines = num_lines_from(CLAMP(cursor.line_ofs, 0, text.size() - 1), get_visible_rows());
  2577. if (cursor.line >= cursor.line_ofs + num_lines && !selection.active) {
  2578. cursor_set_line(cursor.line_ofs + num_lines, false, false);
  2579. }
  2580. }
  2581. void TextEdit::_scroll_lines_down() {
  2582. scrolling = false;
  2583. // calculate the maximum vertical scroll position
  2584. int max_v_scroll = get_total_unhidden_rows();
  2585. if (!scroll_past_end_of_file_enabled) {
  2586. max_v_scroll -= get_visible_rows();
  2587. max_v_scroll = CLAMP(max_v_scroll, 0, get_total_unhidden_rows());
  2588. }
  2589. // adjust the vertical scroll
  2590. if (get_v_scroll() < max_v_scroll) {
  2591. set_v_scroll(get_v_scroll() + 1);
  2592. }
  2593. // adjust the cursor
  2594. if (cursor.line <= cursor.line_ofs - 1 && !selection.active) {
  2595. cursor_set_line(cursor.line_ofs, false, false);
  2596. }
  2597. }
  2598. /**** TEXT EDIT CORE API ****/
  2599. void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
  2600. //save for undo...
  2601. ERR_FAIL_INDEX(p_line, text.size());
  2602. ERR_FAIL_COND(p_char < 0);
  2603. /* STEP 1 add spaces if the char is greater than the end of the line */
  2604. while (p_char > text[p_line].length()) {
  2605. text.set(p_line, text[p_line] + String::chr(' '));
  2606. }
  2607. /* STEP 2 separate dest string in pre and post text */
  2608. String preinsert_text = text[p_line].substr(0, p_char);
  2609. String postinsert_text = text[p_line].substr(p_char, text[p_line].size());
  2610. /* STEP 3 remove \r from source text and separate in substrings */
  2611. //buh bye \r and split
  2612. Vector<String> substrings = p_text.replace("\r", "").split("\n");
  2613. for (int i = 0; i < substrings.size(); i++) {
  2614. //insert the substrings
  2615. if (i == 0) {
  2616. text.set(p_line, preinsert_text + substrings[i]);
  2617. } else {
  2618. text.insert(p_line + i, substrings[i]);
  2619. }
  2620. if (i == substrings.size() - 1) {
  2621. text.set(p_line + i, text[p_line + i] + postinsert_text);
  2622. }
  2623. }
  2624. // if we are just making a new empty line, reset breakpoints and hidden status
  2625. if (p_char == 0 && p_text.replace("\r", "") == "\n") {
  2626. text.set_breakpoint(p_line + 1, text.is_breakpoint(p_line));
  2627. text.set_hidden(p_line + 1, text.is_hidden(p_line));
  2628. text.set_breakpoint(p_line, false);
  2629. text.set_hidden(p_line, false);
  2630. }
  2631. r_end_line = p_line + substrings.size() - 1;
  2632. r_end_column = text[r_end_line].length() - postinsert_text.length();
  2633. if (!text_changed_dirty && !setting_text) {
  2634. if (is_inside_tree())
  2635. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  2636. text_changed_dirty = true;
  2637. }
  2638. }
  2639. String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
  2640. ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
  2641. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
  2642. ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
  2643. ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
  2644. ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // from > to
  2645. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // from > to
  2646. String ret;
  2647. for (int i = p_from_line; i <= p_to_line; i++) {
  2648. int begin = (i == p_from_line) ? p_from_column : 0;
  2649. int end = (i == p_to_line) ? p_to_column : text[i].length();
  2650. if (i > p_from_line)
  2651. ret += "\n";
  2652. ret += text[i].substr(begin, end - begin);
  2653. }
  2654. return ret;
  2655. }
  2656. void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  2657. ERR_FAIL_INDEX(p_from_line, text.size());
  2658. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  2659. ERR_FAIL_INDEX(p_to_line, text.size());
  2660. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  2661. ERR_FAIL_COND(p_to_line < p_from_line); // from > to
  2662. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // from > to
  2663. String pre_text = text[p_from_line].substr(0, p_from_column);
  2664. String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length());
  2665. for (int i = p_from_line; i < p_to_line; i++) {
  2666. text.remove(p_from_line + 1);
  2667. }
  2668. text.set(p_from_line, pre_text + post_text);
  2669. if (!text_changed_dirty && !setting_text) {
  2670. if (is_inside_tree())
  2671. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  2672. text_changed_dirty = true;
  2673. }
  2674. }
  2675. void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) {
  2676. if (!setting_text)
  2677. idle_detect->start();
  2678. if (undo_enabled) {
  2679. _clear_redo();
  2680. }
  2681. int retline, retchar;
  2682. _base_insert_text(p_line, p_char, p_text, retline, retchar);
  2683. if (r_end_line)
  2684. *r_end_line = retline;
  2685. if (r_end_char)
  2686. *r_end_char = retchar;
  2687. if (!undo_enabled)
  2688. return;
  2689. /* UNDO!! */
  2690. TextOperation op;
  2691. op.type = TextOperation::TYPE_INSERT;
  2692. op.from_line = p_line;
  2693. op.from_column = p_char;
  2694. op.to_line = retline;
  2695. op.to_column = retchar;
  2696. op.text = p_text;
  2697. op.version = ++version;
  2698. op.chain_forward = false;
  2699. op.chain_backward = false;
  2700. //see if it shold just be set as current op
  2701. if (current_op.type != op.type) {
  2702. op.prev_version = get_version();
  2703. _push_current_op();
  2704. current_op = op;
  2705. return; //set as current op, return
  2706. }
  2707. //see if it can be merged
  2708. if (current_op.to_line != p_line || current_op.to_column != p_char) {
  2709. op.prev_version = get_version();
  2710. _push_current_op();
  2711. current_op = op;
  2712. return; //set as current op, return
  2713. }
  2714. //merge current op
  2715. current_op.text += p_text;
  2716. current_op.to_column = retchar;
  2717. current_op.to_line = retline;
  2718. current_op.version = op.version;
  2719. }
  2720. void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  2721. if (!setting_text)
  2722. idle_detect->start();
  2723. String text;
  2724. if (undo_enabled) {
  2725. _clear_redo();
  2726. text = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
  2727. }
  2728. _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  2729. if (!undo_enabled)
  2730. return;
  2731. /* UNDO!! */
  2732. TextOperation op;
  2733. op.type = TextOperation::TYPE_REMOVE;
  2734. op.from_line = p_from_line;
  2735. op.from_column = p_from_column;
  2736. op.to_line = p_to_line;
  2737. op.to_column = p_to_column;
  2738. op.text = text;
  2739. op.version = ++version;
  2740. op.chain_forward = false;
  2741. op.chain_backward = false;
  2742. //see if it shold just be set as current op
  2743. if (current_op.type != op.type) {
  2744. op.prev_version = get_version();
  2745. _push_current_op();
  2746. current_op = op;
  2747. return; //set as current op, return
  2748. }
  2749. //see if it can be merged
  2750. if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) {
  2751. //basckace or similar
  2752. current_op.text = text + current_op.text;
  2753. current_op.from_line = p_from_line;
  2754. current_op.from_column = p_from_column;
  2755. return; //update current op
  2756. }
  2757. if (current_op.from_line == p_from_line && current_op.from_column == p_from_column) {
  2758. //current_op.text=text+current_op.text;
  2759. //current_op.from_line=p_from_line;
  2760. //current_op.from_column=p_from_column;
  2761. //return; //update current op
  2762. }
  2763. op.prev_version = get_version();
  2764. _push_current_op();
  2765. current_op = op;
  2766. }
  2767. void TextEdit::_insert_text_at_cursor(const String &p_text) {
  2768. int new_column, new_line;
  2769. _insert_text(cursor.line, cursor.column, p_text, &new_line, &new_column);
  2770. cursor_set_line(new_line);
  2771. cursor_set_column(new_column);
  2772. update();
  2773. }
  2774. int TextEdit::get_char_count() {
  2775. int totalsize = 0;
  2776. for (int i = 0; i < text.size(); i++) {
  2777. if (i > 0)
  2778. totalsize++; // incliude \n
  2779. totalsize += text[i].length();
  2780. }
  2781. return totalsize; // omit last \n
  2782. }
  2783. Size2 TextEdit::get_minimum_size() const {
  2784. return cache.style_normal->get_minimum_size();
  2785. }
  2786. int TextEdit::get_visible_rows() const {
  2787. int total = cache.size.height;
  2788. total -= cache.style_normal->get_minimum_size().height;
  2789. total /= get_row_height();
  2790. return total;
  2791. }
  2792. int TextEdit::get_total_unhidden_rows() const {
  2793. if (!is_hiding_enabled())
  2794. return text.size();
  2795. int total_unhidden = 0;
  2796. for (int i = 0; i < text.size(); i++) {
  2797. if (!text.is_hidden(i))
  2798. total_unhidden++;
  2799. }
  2800. return total_unhidden;
  2801. }
  2802. double TextEdit::get_line_scroll_pos(bool p_recalculate) const {
  2803. if (!is_hiding_enabled())
  2804. return cursor.line_ofs;
  2805. if (!p_recalculate)
  2806. return line_scroll_pos;
  2807. // count num unhidden lines to the cursor line ofs
  2808. double new_line_scroll_pos = 0;
  2809. int to = CLAMP(cursor.line_ofs, 0, text.size() - 1);
  2810. for (int i = 0; i < to; i++) {
  2811. if (!text.is_hidden(i))
  2812. new_line_scroll_pos++;
  2813. }
  2814. return new_line_scroll_pos;
  2815. }
  2816. void TextEdit::update_line_scroll_pos() {
  2817. if (!is_hiding_enabled()) {
  2818. line_scroll_pos = cursor.line_ofs;
  2819. return;
  2820. }
  2821. // count num unhidden lines to the cursor line ofs
  2822. double new_line_scroll_pos = 0;
  2823. int to = CLAMP(cursor.line_ofs, 0, text.size() - 1);
  2824. for (int i = 0; i < to; i++) {
  2825. if (!text.is_hidden(i))
  2826. new_line_scroll_pos++;
  2827. }
  2828. line_scroll_pos = new_line_scroll_pos;
  2829. }
  2830. void TextEdit::adjust_viewport_to_cursor() {
  2831. scrolling = false;
  2832. if (cursor.line_ofs > cursor.line) {
  2833. cursor.line_ofs = cursor.line;
  2834. }
  2835. 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;
  2836. if (v_scroll->is_visible_in_tree())
  2837. visible_width -= v_scroll->get_combined_minimum_size().width;
  2838. visible_width -= 20; // give it a little more space
  2839. int visible_rows = get_visible_rows();
  2840. if (h_scroll->is_visible_in_tree() && !scroll_past_end_of_file_enabled)
  2841. visible_rows -= ((h_scroll->get_combined_minimum_size().height - 1) / get_row_height());
  2842. int num_rows = num_lines_from(CLAMP(cursor.line_ofs, 0, text.size() - 1), MIN(visible_rows, text.size() - 1 - cursor.line_ofs));
  2843. // make sure the cursor is on the screen
  2844. // above the caret
  2845. if (cursor.line > (cursor.line_ofs + MAX(num_rows, visible_rows))) {
  2846. cursor.line_ofs = cursor.line - num_lines_from(cursor.line, -visible_rows) + 1;
  2847. }
  2848. // below the caret
  2849. if (cursor.line_ofs == cursor.line) {
  2850. cursor.line_ofs = cursor.line - 2;
  2851. }
  2852. int line_ofs_max = text.size() - 1;
  2853. if (!scroll_past_end_of_file_enabled) {
  2854. line_ofs_max -= num_lines_from(text.size() - 1, -visible_rows) - 1;
  2855. line_ofs_max += (h_scroll->is_visible_in_tree() ? 1 : 0);
  2856. line_ofs_max += (cursor.line == text.size() - 1 ? 1 : 0);
  2857. }
  2858. line_ofs_max = MAX(line_ofs_max, 0);
  2859. cursor.line_ofs = CLAMP(cursor.line_ofs, 0, line_ofs_max);
  2860. // adjust x offset
  2861. int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]);
  2862. if (cursor_x > (cursor.x_ofs + visible_width))
  2863. cursor.x_ofs = cursor_x - visible_width + 1;
  2864. if (cursor_x < cursor.x_ofs)
  2865. cursor.x_ofs = cursor_x;
  2866. updating_scrolls = true;
  2867. h_scroll->set_value(cursor.x_ofs);
  2868. update_line_scroll_pos();
  2869. double new_v_scroll = get_line_scroll_pos();
  2870. // keep offset if smooth scroll is enabled
  2871. if (smooth_scroll_enabled) {
  2872. new_v_scroll += fmod(v_scroll->get_value(), 1.0);
  2873. }
  2874. v_scroll->set_value(new_v_scroll);
  2875. updating_scrolls = false;
  2876. update();
  2877. }
  2878. void TextEdit::center_viewport_to_cursor() {
  2879. scrolling = false;
  2880. if (cursor.line_ofs > cursor.line)
  2881. cursor.line_ofs = cursor.line;
  2882. if (is_line_hidden(cursor.line))
  2883. unfold_line(cursor.line);
  2884. 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;
  2885. if (v_scroll->is_visible_in_tree())
  2886. visible_width -= v_scroll->get_combined_minimum_size().width;
  2887. visible_width -= 20; // give it a little more space
  2888. int visible_rows = get_visible_rows();
  2889. if (h_scroll->is_visible_in_tree())
  2890. visible_rows -= ((h_scroll->get_combined_minimum_size().height - 1) / get_row_height());
  2891. int max_ofs = text.size() - (scroll_past_end_of_file_enabled ? 1 : num_lines_from(text.size() - 1, -visible_rows));
  2892. cursor.line_ofs = CLAMP(cursor.line - num_lines_from(cursor.line - visible_rows / 2, -visible_rows / 2), 0, max_ofs);
  2893. int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]);
  2894. if (cursor_x > (cursor.x_ofs + visible_width))
  2895. cursor.x_ofs = cursor_x - visible_width + 1;
  2896. if (cursor_x < cursor.x_ofs)
  2897. cursor.x_ofs = cursor_x;
  2898. updating_scrolls = true;
  2899. h_scroll->set_value(cursor.x_ofs);
  2900. update_line_scroll_pos();
  2901. double new_v_scroll = get_line_scroll_pos();
  2902. // keep offset if smooth scroll is enabled
  2903. if (smooth_scroll_enabled) {
  2904. new_v_scroll += fmod(v_scroll->get_value(), 1.0);
  2905. }
  2906. v_scroll->set_value(new_v_scroll);
  2907. updating_scrolls = false;
  2908. update();
  2909. }
  2910. void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
  2911. if (p_col < 0)
  2912. p_col = 0;
  2913. cursor.column = p_col;
  2914. if (cursor.column > get_line(cursor.line).length())
  2915. cursor.column = get_line(cursor.line).length();
  2916. cursor.last_fit_x = get_column_x_offset(cursor.column, get_line(cursor.line));
  2917. if (p_adjust_viewport)
  2918. adjust_viewport_to_cursor();
  2919. if (!cursor_changed_dirty) {
  2920. if (is_inside_tree())
  2921. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  2922. cursor_changed_dirty = true;
  2923. }
  2924. }
  2925. void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport, bool p_can_be_hidden) {
  2926. if (setting_row)
  2927. return;
  2928. setting_row = true;
  2929. if (p_row < 0)
  2930. p_row = 0;
  2931. if (p_row >= (int)text.size())
  2932. p_row = (int)text.size() - 1;
  2933. if (!p_can_be_hidden) {
  2934. if (is_line_hidden(CLAMP(p_row, 0, text.size() - 1))) {
  2935. int move_down = num_lines_from(p_row, 1) - 1;
  2936. if (p_row + move_down <= text.size() - 1 && !is_line_hidden(p_row + move_down)) {
  2937. p_row += move_down;
  2938. } else {
  2939. int move_up = num_lines_from(p_row, -1) - 1;
  2940. if (p_row - move_up > 0 && !is_line_hidden(p_row - move_up)) {
  2941. p_row -= move_up;
  2942. } else {
  2943. WARN_PRINTS(("Cursor set to hidden line " + itos(p_row) + " and there are no nonhidden lines."));
  2944. }
  2945. }
  2946. }
  2947. }
  2948. cursor.line = p_row;
  2949. cursor.column = get_char_pos_for(cursor.last_fit_x, get_line(cursor.line));
  2950. if (p_adjust_viewport)
  2951. adjust_viewport_to_cursor();
  2952. setting_row = false;
  2953. if (!cursor_changed_dirty) {
  2954. if (is_inside_tree())
  2955. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  2956. cursor_changed_dirty = true;
  2957. }
  2958. }
  2959. int TextEdit::cursor_get_column() const {
  2960. return cursor.column;
  2961. }
  2962. int TextEdit::cursor_get_line() const {
  2963. return cursor.line;
  2964. }
  2965. bool TextEdit::cursor_get_blink_enabled() const {
  2966. return caret_blink_enabled;
  2967. }
  2968. void TextEdit::cursor_set_blink_enabled(const bool p_enabled) {
  2969. caret_blink_enabled = p_enabled;
  2970. if (p_enabled) {
  2971. caret_blink_timer->start();
  2972. } else {
  2973. caret_blink_timer->stop();
  2974. }
  2975. draw_caret = true;
  2976. }
  2977. float TextEdit::cursor_get_blink_speed() const {
  2978. return caret_blink_timer->get_wait_time();
  2979. }
  2980. void TextEdit::cursor_set_blink_speed(const float p_speed) {
  2981. ERR_FAIL_COND(p_speed <= 0);
  2982. caret_blink_timer->set_wait_time(p_speed);
  2983. }
  2984. void TextEdit::cursor_set_block_mode(const bool p_enable) {
  2985. block_caret = p_enable;
  2986. update();
  2987. }
  2988. bool TextEdit::cursor_is_block_mode() const {
  2989. return block_caret;
  2990. }
  2991. void TextEdit::set_right_click_moves_caret(bool p_enable) {
  2992. right_click_moves_caret = p_enable;
  2993. }
  2994. bool TextEdit::is_right_click_moving_caret() const {
  2995. return right_click_moves_caret;
  2996. }
  2997. void TextEdit::_v_scroll_input() {
  2998. scrolling = false;
  2999. }
  3000. void TextEdit::_scroll_moved(double p_to_val) {
  3001. if (updating_scrolls)
  3002. return;
  3003. if (h_scroll->is_visible_in_tree())
  3004. cursor.x_ofs = h_scroll->get_value();
  3005. if (v_scroll->is_visible_in_tree()) {
  3006. double val = v_scroll->get_value();
  3007. cursor.line_ofs = num_lines_from(0, (int)floor(val));
  3008. line_scroll_pos = (int)floor(val);
  3009. }
  3010. update();
  3011. }
  3012. int TextEdit::get_row_height() const {
  3013. return cache.font->get_height() + cache.line_spacing;
  3014. }
  3015. int TextEdit::get_char_pos_for(int p_px, String p_str) const {
  3016. int px = 0;
  3017. int c = 0;
  3018. int tab_w = cache.font->get_char_size(' ').width * indent_size;
  3019. while (c < p_str.length()) {
  3020. int w = 0;
  3021. if (p_str[c] == '\t') {
  3022. int left = px % tab_w;
  3023. if (left == 0)
  3024. w = tab_w;
  3025. else
  3026. w = tab_w - px % tab_w; // is right...
  3027. } else {
  3028. w = cache.font->get_char_size(p_str[c], p_str[c + 1]).width;
  3029. }
  3030. if (p_px < (px + w / 2))
  3031. break;
  3032. px += w;
  3033. c++;
  3034. }
  3035. return c;
  3036. }
  3037. int TextEdit::get_column_x_offset(int p_char, String p_str) {
  3038. int px = 0;
  3039. int tab_w = cache.font->get_char_size(' ').width * indent_size;
  3040. for (int i = 0; i < p_char; i++) {
  3041. if (i >= p_str.length())
  3042. break;
  3043. if (p_str[i] == '\t') {
  3044. int left = px % tab_w;
  3045. if (left == 0)
  3046. px += tab_w;
  3047. else
  3048. px += tab_w - px % tab_w; // is right...
  3049. } else {
  3050. px += cache.font->get_char_size(p_str[i], p_str[i + 1]).width;
  3051. }
  3052. }
  3053. return px;
  3054. }
  3055. void TextEdit::insert_text_at_cursor(const String &p_text) {
  3056. if (selection.active) {
  3057. cursor_set_line(selection.from_line);
  3058. cursor_set_column(selection.from_column);
  3059. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3060. selection.active = false;
  3061. selection.selecting_mode = Selection::MODE_NONE;
  3062. }
  3063. _insert_text_at_cursor(p_text);
  3064. update();
  3065. }
  3066. Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
  3067. if (highlighted_word != String())
  3068. return CURSOR_POINTING_HAND;
  3069. int gutter = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width;
  3070. if ((completion_active && completion_rect.has_point(p_pos))) {
  3071. return CURSOR_ARROW;
  3072. }
  3073. if (p_pos.x < gutter) {
  3074. int row, col;
  3075. _get_mouse_pos(p_pos, row, col);
  3076. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  3077. // breakpoint icon
  3078. if (draw_breakpoint_gutter && p_pos.x > left_margin && p_pos.x <= left_margin + cache.breakpoint_gutter_width + 3) {
  3079. return CURSOR_POINTING_HAND;
  3080. }
  3081. // fold icon
  3082. int gutter_left = left_margin + cache.breakpoint_gutter_width + cache.line_number_w;
  3083. if (draw_fold_gutter && p_pos.x > gutter_left - 6 && p_pos.x <= gutter_left + cache.fold_gutter_width - 3) {
  3084. if (is_folded(row) || can_fold(row))
  3085. return CURSOR_POINTING_HAND;
  3086. else
  3087. return CURSOR_ARROW;
  3088. }
  3089. return CURSOR_ARROW;
  3090. } else {
  3091. int row, col;
  3092. _get_mouse_pos(p_pos, row, col);
  3093. // eol fold icon
  3094. if (is_folded(row)) {
  3095. int line_width = text.get_line_width(row);
  3096. line_width += cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width - cursor.x_ofs;
  3097. if (p_pos.x > line_width - 3 && p_pos.x <= line_width + cache.folded_eol_icon->get_width() + 3) {
  3098. return CURSOR_POINTING_HAND;
  3099. }
  3100. }
  3101. }
  3102. return CURSOR_IBEAM;
  3103. }
  3104. void TextEdit::set_text(String p_text) {
  3105. setting_text = true;
  3106. clear();
  3107. _insert_text_at_cursor(p_text);
  3108. clear_undo_history();
  3109. cursor.column = 0;
  3110. cursor.line = 0;
  3111. cursor.x_ofs = 0;
  3112. cursor.line_ofs = 0;
  3113. line_scroll_pos = 0;
  3114. cursor.last_fit_x = 0;
  3115. cursor_set_line(0);
  3116. cursor_set_column(0);
  3117. update();
  3118. setting_text = false;
  3119. _text_changed_emit();
  3120. //get_range()->set(0);
  3121. };
  3122. String TextEdit::get_text() {
  3123. String longthing;
  3124. int len = text.size();
  3125. for (int i = 0; i < len; i++) {
  3126. longthing += text[i];
  3127. if (i != len - 1)
  3128. longthing += "\n";
  3129. }
  3130. return longthing;
  3131. };
  3132. String TextEdit::get_text_for_lookup_completion() {
  3133. int row, col;
  3134. _get_mouse_pos(get_local_mouse_position(), row, col);
  3135. String longthing;
  3136. int len = text.size();
  3137. for (int i = 0; i < len; i++) {
  3138. if (i == row) {
  3139. longthing += text[i].substr(0, col);
  3140. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  3141. longthing += text[i].substr(col, text[i].size());
  3142. } else {
  3143. longthing += text[i];
  3144. }
  3145. if (i != len - 1)
  3146. longthing += "\n";
  3147. }
  3148. return longthing;
  3149. }
  3150. String TextEdit::get_text_for_completion() {
  3151. String longthing;
  3152. int len = text.size();
  3153. for (int i = 0; i < len; i++) {
  3154. if (i == cursor.line) {
  3155. longthing += text[i].substr(0, cursor.column);
  3156. longthing += String::chr(0xFFFF); //not unicode, represents the cursor
  3157. longthing += text[i].substr(cursor.column, text[i].size());
  3158. } else {
  3159. longthing += text[i];
  3160. }
  3161. if (i != len - 1)
  3162. longthing += "\n";
  3163. }
  3164. return longthing;
  3165. };
  3166. String TextEdit::get_line(int line) const {
  3167. if (line < 0 || line >= text.size())
  3168. return "";
  3169. return text[line];
  3170. };
  3171. void TextEdit::_clear() {
  3172. clear_undo_history();
  3173. text.clear();
  3174. cursor.column = 0;
  3175. cursor.line = 0;
  3176. cursor.x_ofs = 0;
  3177. cursor.line_ofs = 0;
  3178. line_scroll_pos = 0;
  3179. cursor.last_fit_x = 0;
  3180. }
  3181. void TextEdit::clear() {
  3182. setting_text = true;
  3183. _clear();
  3184. setting_text = false;
  3185. };
  3186. void TextEdit::set_readonly(bool p_readonly) {
  3187. readonly = p_readonly;
  3188. update();
  3189. }
  3190. bool TextEdit::is_readonly() const {
  3191. return readonly;
  3192. }
  3193. void TextEdit::set_wrap(bool p_wrap) {
  3194. wrap = p_wrap;
  3195. }
  3196. void TextEdit::set_max_chars(int p_max_chars) {
  3197. max_chars = p_max_chars;
  3198. }
  3199. void TextEdit::_reset_caret_blink_timer() {
  3200. if (caret_blink_enabled) {
  3201. caret_blink_timer->stop();
  3202. caret_blink_timer->start();
  3203. draw_caret = true;
  3204. update();
  3205. }
  3206. }
  3207. void TextEdit::_toggle_draw_caret() {
  3208. draw_caret = !draw_caret;
  3209. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  3210. update();
  3211. }
  3212. }
  3213. void TextEdit::_update_caches() {
  3214. cache.style_normal = get_stylebox("normal");
  3215. cache.style_focus = get_stylebox("focus");
  3216. cache.style_readonly = get_stylebox("read_only");
  3217. cache.completion_background_color = get_color("completion_background_color");
  3218. cache.completion_selected_color = get_color("completion_selected_color");
  3219. cache.completion_existing_color = get_color("completion_existing_color");
  3220. cache.completion_font_color = get_color("completion_font_color");
  3221. cache.font = get_font("font");
  3222. cache.caret_color = get_color("caret_color");
  3223. cache.caret_background_color = get_color("caret_background_color");
  3224. cache.line_number_color = get_color("line_number_color");
  3225. cache.font_color = get_color("font_color");
  3226. cache.font_selected_color = get_color("font_selected_color");
  3227. cache.keyword_color = get_color("keyword_color");
  3228. cache.function_color = get_color("function_color");
  3229. cache.member_variable_color = get_color("member_variable_color");
  3230. cache.number_color = get_color("number_color");
  3231. cache.selection_color = get_color("selection_color");
  3232. cache.mark_color = get_color("mark_color");
  3233. cache.current_line_color = get_color("current_line_color");
  3234. cache.line_length_guideline_color = get_color("line_length_guideline_color");
  3235. cache.breakpoint_color = get_color("breakpoint_color");
  3236. cache.code_folding_color = get_color("code_folding_color");
  3237. cache.brace_mismatch_color = get_color("brace_mismatch_color");
  3238. cache.word_highlighted_color = get_color("word_highlighted_color");
  3239. cache.search_result_color = get_color("search_result_color");
  3240. cache.search_result_border_color = get_color("search_result_border_color");
  3241. cache.symbol_color = get_color("symbol_color");
  3242. cache.background_color = get_color("background_color");
  3243. cache.line_spacing = get_constant("line_spacing");
  3244. cache.row_height = cache.font->get_height() + cache.line_spacing;
  3245. cache.tab_icon = get_icon("tab");
  3246. cache.folded_icon = get_icon("GuiTreeArrowRight", "EditorIcons");
  3247. cache.can_fold_icon = get_icon("GuiTreeArrowDown", "EditorIcons");
  3248. cache.folded_eol_icon = get_icon("GuiEllipsis", "EditorIcons");
  3249. text.set_font(cache.font);
  3250. }
  3251. void TextEdit::clear_colors() {
  3252. keywords.clear();
  3253. color_regions.clear();
  3254. text.clear_caches();
  3255. }
  3256. void TextEdit::add_keyword_color(const String &p_keyword, const Color &p_color) {
  3257. keywords[p_keyword] = p_color;
  3258. update();
  3259. }
  3260. void TextEdit::add_color_region(const String &p_begin_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  3261. color_regions.push_back(ColorRegion(p_begin_key, p_end_key, p_color, p_line_only));
  3262. text.clear_caches();
  3263. update();
  3264. }
  3265. void TextEdit::set_syntax_coloring(bool p_enabled) {
  3266. syntax_coloring = p_enabled;
  3267. update();
  3268. }
  3269. bool TextEdit::is_syntax_coloring_enabled() const {
  3270. return syntax_coloring;
  3271. }
  3272. void TextEdit::set_auto_indent(bool p_auto_indent) {
  3273. auto_indent = p_auto_indent;
  3274. }
  3275. void TextEdit::cut() {
  3276. if (!selection.active) {
  3277. String clipboard = text[cursor.line];
  3278. OS::get_singleton()->set_clipboard(clipboard);
  3279. cursor_set_line(cursor.line);
  3280. cursor_set_column(0);
  3281. _remove_text(cursor.line, 0, cursor.line, text[cursor.line].length());
  3282. backspace_at_cursor();
  3283. update();
  3284. cursor_set_line(cursor.line + 1);
  3285. cut_copy_line = clipboard;
  3286. } else {
  3287. String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3288. OS::get_singleton()->set_clipboard(clipboard);
  3289. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3290. cursor_set_line(selection.from_line); // set afterwards else it causes the view to be offset
  3291. cursor_set_column(selection.from_column);
  3292. selection.active = false;
  3293. selection.selecting_mode = Selection::MODE_NONE;
  3294. update();
  3295. cut_copy_line = "";
  3296. }
  3297. }
  3298. void TextEdit::copy() {
  3299. if (!selection.active) {
  3300. String clipboard = _base_get_text(cursor.line, 0, cursor.line, text[cursor.line].length());
  3301. OS::get_singleton()->set_clipboard(clipboard);
  3302. cut_copy_line = clipboard;
  3303. } else {
  3304. String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3305. OS::get_singleton()->set_clipboard(clipboard);
  3306. cut_copy_line = "";
  3307. }
  3308. }
  3309. void TextEdit::paste() {
  3310. String clipboard = OS::get_singleton()->get_clipboard();
  3311. if (selection.active) {
  3312. selection.active = false;
  3313. selection.selecting_mode = Selection::MODE_NONE;
  3314. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3315. cursor_set_line(selection.from_line);
  3316. cursor_set_column(selection.from_column);
  3317. } else if (!cut_copy_line.empty() && cut_copy_line == clipboard) {
  3318. cursor_set_column(0);
  3319. String ins = "\n";
  3320. clipboard += ins;
  3321. }
  3322. _insert_text_at_cursor(clipboard);
  3323. update();
  3324. }
  3325. void TextEdit::select_all() {
  3326. if (text.size() == 1 && text[0].length() == 0)
  3327. return;
  3328. selection.active = true;
  3329. selection.from_line = 0;
  3330. selection.from_column = 0;
  3331. selection.selecting_line = 0;
  3332. selection.selecting_column = 0;
  3333. selection.to_line = text.size() - 1;
  3334. selection.to_column = text[selection.to_line].length();
  3335. selection.selecting_mode = Selection::MODE_SHIFT;
  3336. selection.shiftclick_left = true;
  3337. cursor_set_line(selection.to_line, false);
  3338. cursor_set_column(selection.to_column, false);
  3339. update();
  3340. }
  3341. void TextEdit::deselect() {
  3342. selection.active = false;
  3343. update();
  3344. }
  3345. void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3346. if (p_from_line >= text.size())
  3347. p_from_line = text.size() - 1;
  3348. if (p_from_column >= text[p_from_line].length())
  3349. p_from_column = text[p_from_line].length();
  3350. if (p_to_line >= text.size())
  3351. p_to_line = text.size() - 1;
  3352. if (p_to_column >= text[p_to_line].length())
  3353. p_to_column = text[p_to_line].length();
  3354. selection.from_line = p_from_line;
  3355. selection.from_column = p_from_column;
  3356. selection.to_line = p_to_line;
  3357. selection.to_column = p_to_column;
  3358. selection.active = true;
  3359. if (selection.from_line == selection.to_line) {
  3360. if (selection.from_column == selection.to_column) {
  3361. selection.active = false;
  3362. } else if (selection.from_column > selection.to_column) {
  3363. selection.shiftclick_left = false;
  3364. SWAP(selection.from_column, selection.to_column);
  3365. } else {
  3366. selection.shiftclick_left = true;
  3367. }
  3368. } else if (selection.from_line > selection.to_line) {
  3369. selection.shiftclick_left = false;
  3370. SWAP(selection.from_line, selection.to_line);
  3371. SWAP(selection.from_column, selection.to_column);
  3372. } else {
  3373. selection.shiftclick_left = true;
  3374. }
  3375. update();
  3376. }
  3377. void TextEdit::swap_lines(int line1, int line2) {
  3378. String tmp = get_line(line1);
  3379. String tmp2 = get_line(line2);
  3380. set_line(line2, tmp);
  3381. set_line(line1, tmp2);
  3382. }
  3383. bool TextEdit::is_selection_active() const {
  3384. return selection.active;
  3385. }
  3386. int TextEdit::get_selection_from_line() const {
  3387. ERR_FAIL_COND_V(!selection.active, -1);
  3388. return selection.from_line;
  3389. }
  3390. int TextEdit::get_selection_from_column() const {
  3391. ERR_FAIL_COND_V(!selection.active, -1);
  3392. return selection.from_column;
  3393. }
  3394. int TextEdit::get_selection_to_line() const {
  3395. ERR_FAIL_COND_V(!selection.active, -1);
  3396. return selection.to_line;
  3397. }
  3398. int TextEdit::get_selection_to_column() const {
  3399. ERR_FAIL_COND_V(!selection.active, -1);
  3400. return selection.to_column;
  3401. }
  3402. String TextEdit::get_selection_text() const {
  3403. if (!selection.active)
  3404. return "";
  3405. return _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3406. }
  3407. String TextEdit::get_word_under_cursor() const {
  3408. int prev_cc = cursor.column;
  3409. while (prev_cc > 0) {
  3410. bool is_char = _is_text_char(text[cursor.line][prev_cc - 1]);
  3411. if (!is_char)
  3412. break;
  3413. --prev_cc;
  3414. }
  3415. int next_cc = cursor.column;
  3416. while (next_cc < text[cursor.line].length()) {
  3417. bool is_char = _is_text_char(text[cursor.line][next_cc]);
  3418. if (!is_char)
  3419. break;
  3420. ++next_cc;
  3421. }
  3422. if (prev_cc == cursor.column || next_cc == cursor.column)
  3423. return "";
  3424. return text[cursor.line].substr(prev_cc, next_cc - prev_cc);
  3425. }
  3426. void TextEdit::set_search_text(const String &p_search_text) {
  3427. search_text = p_search_text;
  3428. }
  3429. void TextEdit::set_search_flags(uint32_t p_flags) {
  3430. search_flags = p_flags;
  3431. }
  3432. void TextEdit::set_current_search_result(int line, int col) {
  3433. search_result_line = line;
  3434. search_result_col = col;
  3435. update();
  3436. }
  3437. void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
  3438. highlight_all_occurrences = p_enabled;
  3439. update();
  3440. }
  3441. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  3442. return highlight_all_occurrences;
  3443. }
  3444. int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) {
  3445. int col = -1;
  3446. if (p_key.length() > 0 && p_search.length() > 0) {
  3447. if (p_from_column < 0 || p_from_column > p_search.length()) {
  3448. p_from_column = 0;
  3449. }
  3450. while (col == -1 && p_from_column <= p_search.length()) {
  3451. if (p_search_flags & SEARCH_MATCH_CASE) {
  3452. col = p_search.find(p_key, p_from_column);
  3453. } else {
  3454. col = p_search.findn(p_key, p_from_column);
  3455. }
  3456. // whole words only
  3457. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
  3458. p_from_column = col;
  3459. if (col > 0 && _is_text_char(p_search[col - 1])) {
  3460. col = -1;
  3461. } else if ((col + p_key.length()) < p_search.length() && _is_text_char(p_search[col + p_key.length()])) {
  3462. col = -1;
  3463. }
  3464. }
  3465. p_from_column += 1;
  3466. }
  3467. }
  3468. return col;
  3469. }
  3470. PoolVector<int> TextEdit::_search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
  3471. int col, line;
  3472. if (search(p_key, p_search_flags, p_from_line, p_from_column, col, line)) {
  3473. PoolVector<int> result;
  3474. result.resize(2);
  3475. result.set(0, line);
  3476. result.set(1, col);
  3477. return result;
  3478. } else {
  3479. return PoolVector<int>();
  3480. }
  3481. }
  3482. 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 {
  3483. if (p_key.length() == 0)
  3484. return false;
  3485. ERR_FAIL_INDEX_V(p_from_line, text.size(), false);
  3486. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, false);
  3487. //search through the whole documment, but start by current line
  3488. int line = p_from_line;
  3489. int pos = -1;
  3490. for (int i = 0; i < text.size() + 1; i++) {
  3491. //backwards is broken...
  3492. //int idx=(p_search_flags&SEARCH_BACKWARDS)?(text.size()-i):i; //do backwards seearch
  3493. if (line < 0) {
  3494. line = text.size() - 1;
  3495. }
  3496. if (line == text.size()) {
  3497. line = 0;
  3498. }
  3499. String text_line = text[line];
  3500. int from_column = 0;
  3501. if (line == p_from_line) {
  3502. if (i == text.size()) {
  3503. //wrapped
  3504. if (p_search_flags & SEARCH_BACKWARDS) {
  3505. from_column = text_line.length();
  3506. } else {
  3507. from_column = 0;
  3508. }
  3509. } else {
  3510. from_column = p_from_column;
  3511. }
  3512. } else {
  3513. if (p_search_flags & SEARCH_BACKWARDS)
  3514. from_column = text_line.length() - 1;
  3515. else
  3516. from_column = 0;
  3517. }
  3518. pos = -1;
  3519. int pos_from = 0;
  3520. int last_pos = -1;
  3521. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) {
  3522. if (p_search_flags & SEARCH_BACKWARDS) {
  3523. if (last_pos > from_column)
  3524. break;
  3525. pos = last_pos;
  3526. } else {
  3527. if (last_pos >= from_column) {
  3528. pos = last_pos;
  3529. break;
  3530. }
  3531. }
  3532. pos_from = last_pos + p_key.length();
  3533. }
  3534. if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) {
  3535. //validate for whole words
  3536. if (pos > 0 && _is_text_char(text_line[pos - 1]))
  3537. pos = -1;
  3538. else if (_is_text_char(text_line[pos + p_key.length()]))
  3539. pos = -1;
  3540. }
  3541. if (pos != -1)
  3542. break;
  3543. if (p_search_flags & SEARCH_BACKWARDS)
  3544. line--;
  3545. else
  3546. line++;
  3547. }
  3548. if (pos == -1) {
  3549. r_line = -1;
  3550. r_column = -1;
  3551. return false;
  3552. }
  3553. r_line = line;
  3554. r_column = pos;
  3555. return true;
  3556. }
  3557. void TextEdit::_cursor_changed_emit() {
  3558. emit_signal("cursor_changed");
  3559. cursor_changed_dirty = false;
  3560. }
  3561. void TextEdit::_text_changed_emit() {
  3562. emit_signal("text_changed");
  3563. text_changed_dirty = false;
  3564. }
  3565. void TextEdit::set_line_as_marked(int p_line, bool p_marked) {
  3566. ERR_FAIL_INDEX(p_line, text.size());
  3567. text.set_marked(p_line, p_marked);
  3568. update();
  3569. }
  3570. bool TextEdit::is_line_set_as_breakpoint(int p_line) const {
  3571. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3572. return text.is_breakpoint(p_line);
  3573. }
  3574. void TextEdit::set_line_as_breakpoint(int p_line, bool p_breakpoint) {
  3575. ERR_FAIL_INDEX(p_line, text.size());
  3576. text.set_breakpoint(p_line, p_breakpoint);
  3577. update();
  3578. }
  3579. void TextEdit::get_breakpoints(List<int> *p_breakpoints) const {
  3580. for (int i = 0; i < text.size(); i++) {
  3581. if (text.is_breakpoint(i))
  3582. p_breakpoints->push_back(i);
  3583. }
  3584. }
  3585. void TextEdit::set_line_as_hidden(int p_line, bool p_hidden) {
  3586. ERR_FAIL_INDEX(p_line, text.size());
  3587. if (is_hiding_enabled() || !p_hidden)
  3588. text.set_hidden(p_line, p_hidden);
  3589. update();
  3590. }
  3591. bool TextEdit::is_line_hidden(int p_line) const {
  3592. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3593. return text.is_hidden(p_line);
  3594. }
  3595. void TextEdit::fold_all_lines() {
  3596. for (int i = 0; i < text.size(); i++) {
  3597. fold_line(i);
  3598. }
  3599. _update_scrollbars();
  3600. update();
  3601. }
  3602. void TextEdit::unhide_all_lines() {
  3603. for (int i = 0; i < text.size(); i++) {
  3604. text.set_hidden(i, false);
  3605. }
  3606. _update_scrollbars();
  3607. update();
  3608. }
  3609. int TextEdit::num_lines_from(int p_line_from, int unhidden_amount) const {
  3610. // returns the number of hidden and unhidden lines from p_line_from to p_line_from + amount of visible lines
  3611. ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(unhidden_amount));
  3612. if (!is_hiding_enabled())
  3613. return ABS(unhidden_amount);
  3614. int num_visible = 0;
  3615. int num_total = 0;
  3616. if (unhidden_amount >= 0) {
  3617. for (int i = p_line_from; i < text.size(); i++) {
  3618. num_total++;
  3619. if (!is_line_hidden(i))
  3620. num_visible++;
  3621. if (num_visible >= unhidden_amount)
  3622. break;
  3623. }
  3624. } else {
  3625. unhidden_amount = ABS(unhidden_amount);
  3626. for (int i = p_line_from; i >= 0; i--) {
  3627. num_total++;
  3628. if (!is_line_hidden(i))
  3629. num_visible++;
  3630. if (num_visible >= unhidden_amount)
  3631. break;
  3632. }
  3633. }
  3634. return num_total;
  3635. }
  3636. int TextEdit::get_indent_level(int p_line) const {
  3637. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3638. // counts number of tabs and spaces before line starts
  3639. int tab_count = 0;
  3640. int whitespace_count = 0;
  3641. int line_length = text[p_line].size();
  3642. for (int i = 0; i < line_length - 1; i++) {
  3643. if (text[p_line][i] == '\t') {
  3644. tab_count++;
  3645. } else if (text[p_line][i] == ' ') {
  3646. whitespace_count++;
  3647. } else if (text[p_line][i] == '#') {
  3648. break;
  3649. } else {
  3650. break;
  3651. }
  3652. }
  3653. return tab_count + whitespace_count / indent_size;
  3654. }
  3655. bool TextEdit::can_fold(int p_line) const {
  3656. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3657. if (!is_hiding_enabled())
  3658. return false;
  3659. if (p_line + 1 >= text.size())
  3660. return false;
  3661. if (text[p_line].size() == 0)
  3662. return false;
  3663. if (is_folded(p_line))
  3664. return false;
  3665. if (is_line_hidden(p_line))
  3666. return false;
  3667. int start_indent = get_indent_level(p_line);
  3668. for (int i = p_line + 1; i < text.size(); i++) {
  3669. if (text[i].size() == 0)
  3670. continue;
  3671. int next_indent = get_indent_level(i);
  3672. if (next_indent > start_indent)
  3673. return true;
  3674. else
  3675. return false;
  3676. }
  3677. return false;
  3678. }
  3679. bool TextEdit::is_folded(int p_line) const {
  3680. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  3681. if (p_line + 1 >= text.size())
  3682. return false;
  3683. if (!is_line_hidden(p_line) && is_line_hidden(p_line + 1))
  3684. return true;
  3685. return false;
  3686. }
  3687. void TextEdit::fold_line(int p_line) {
  3688. ERR_FAIL_INDEX(p_line, text.size());
  3689. if (!is_hiding_enabled())
  3690. return;
  3691. if (!can_fold(p_line))
  3692. return;
  3693. // hide lines below this one
  3694. int start_indent = get_indent_level(p_line);
  3695. int last_line = start_indent;
  3696. for (int i = p_line + 1; i < text.size(); i++) {
  3697. if (text[i].strip_edges().size() != 0) {
  3698. if (get_indent_level(i) > start_indent) {
  3699. last_line = i;
  3700. } else {
  3701. break;
  3702. }
  3703. }
  3704. }
  3705. for (int i = p_line + 1; i <= last_line; i++) {
  3706. set_line_as_hidden(i, true);
  3707. }
  3708. // fix selection
  3709. if (is_selection_active()) {
  3710. if (is_line_hidden(selection.from_line) && is_line_hidden(selection.to_line)) {
  3711. deselect();
  3712. } else if (is_line_hidden(selection.from_line)) {
  3713. select(p_line, 9999, selection.to_line, selection.to_column);
  3714. } else if (is_line_hidden(selection.to_line)) {
  3715. select(selection.from_line, selection.from_column, p_line, 9999);
  3716. }
  3717. }
  3718. // reset cursor
  3719. if (is_line_hidden(cursor.line)) {
  3720. cursor_set_line(p_line, false, false);
  3721. cursor_set_column(get_line(p_line).length(), false);
  3722. }
  3723. _update_scrollbars();
  3724. update();
  3725. }
  3726. void TextEdit::unfold_line(int p_line) {
  3727. ERR_FAIL_INDEX(p_line, text.size());
  3728. if (!is_folded(p_line) && !is_line_hidden(p_line))
  3729. return;
  3730. int fold_start = p_line;
  3731. for (fold_start = p_line; fold_start > 0; fold_start--) {
  3732. if (is_folded(fold_start))
  3733. break;
  3734. }
  3735. fold_start = is_folded(fold_start) ? fold_start : p_line;
  3736. for (int i = fold_start + 1; i < text.size(); i++) {
  3737. if (is_line_hidden(i)) {
  3738. set_line_as_hidden(i, false);
  3739. } else {
  3740. break;
  3741. }
  3742. }
  3743. _update_scrollbars();
  3744. update();
  3745. }
  3746. void TextEdit::toggle_fold_line(int p_line) {
  3747. ERR_FAIL_INDEX(p_line, text.size());
  3748. if (!is_folded(p_line))
  3749. fold_line(p_line);
  3750. else
  3751. unfold_line(p_line);
  3752. }
  3753. int TextEdit::get_line_count() const {
  3754. return text.size();
  3755. }
  3756. void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
  3757. ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
  3758. bool insert = p_op.type == TextOperation::TYPE_INSERT;
  3759. if (p_reverse)
  3760. insert = !insert;
  3761. if (insert) {
  3762. int check_line;
  3763. int check_column;
  3764. _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
  3765. ERR_FAIL_COND(check_line != p_op.to_line); // BUG
  3766. ERR_FAIL_COND(check_column != p_op.to_column); // BUG
  3767. } else {
  3768. _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
  3769. }
  3770. }
  3771. void TextEdit::_clear_redo() {
  3772. if (undo_stack_pos == NULL)
  3773. return; //nothing to clear
  3774. _push_current_op();
  3775. while (undo_stack_pos) {
  3776. List<TextOperation>::Element *elem = undo_stack_pos;
  3777. undo_stack_pos = undo_stack_pos->next();
  3778. undo_stack.erase(elem);
  3779. }
  3780. }
  3781. void TextEdit::undo() {
  3782. _push_current_op();
  3783. if (undo_stack_pos == NULL) {
  3784. if (!undo_stack.size())
  3785. return; //nothing to undo
  3786. undo_stack_pos = undo_stack.back();
  3787. } else if (undo_stack_pos == undo_stack.front())
  3788. return; // at the bottom of the undo stack
  3789. else
  3790. undo_stack_pos = undo_stack_pos->prev();
  3791. TextOperation op = undo_stack_pos->get();
  3792. _do_text_op(op, true);
  3793. current_op.version = op.prev_version;
  3794. if (undo_stack_pos->get().chain_backward) {
  3795. while (true) {
  3796. ERR_BREAK(!undo_stack_pos->prev());
  3797. undo_stack_pos = undo_stack_pos->prev();
  3798. op = undo_stack_pos->get();
  3799. _do_text_op(op, true);
  3800. current_op.version = op.prev_version;
  3801. if (undo_stack_pos->get().chain_forward) {
  3802. break;
  3803. }
  3804. }
  3805. }
  3806. if (undo_stack_pos->get().type == TextOperation::TYPE_REMOVE) {
  3807. cursor_set_line(undo_stack_pos->get().to_line);
  3808. cursor_set_column(undo_stack_pos->get().to_column);
  3809. _cancel_code_hint();
  3810. } else {
  3811. cursor_set_line(undo_stack_pos->get().from_line);
  3812. cursor_set_column(undo_stack_pos->get().from_column);
  3813. }
  3814. update();
  3815. }
  3816. void TextEdit::redo() {
  3817. _push_current_op();
  3818. if (undo_stack_pos == NULL)
  3819. return; //nothing to do.
  3820. TextOperation op = undo_stack_pos->get();
  3821. _do_text_op(op, false);
  3822. current_op.version = op.version;
  3823. if (undo_stack_pos->get().chain_forward) {
  3824. while (true) {
  3825. ERR_BREAK(!undo_stack_pos->next());
  3826. undo_stack_pos = undo_stack_pos->next();
  3827. op = undo_stack_pos->get();
  3828. _do_text_op(op, false);
  3829. current_op.version = op.version;
  3830. if (undo_stack_pos->get().chain_backward)
  3831. break;
  3832. }
  3833. }
  3834. cursor_set_line(undo_stack_pos->get().to_line);
  3835. cursor_set_column(undo_stack_pos->get().to_column);
  3836. undo_stack_pos = undo_stack_pos->next();
  3837. update();
  3838. }
  3839. void TextEdit::clear_undo_history() {
  3840. saved_version = 0;
  3841. current_op.type = TextOperation::TYPE_NONE;
  3842. undo_stack_pos = NULL;
  3843. undo_stack.clear();
  3844. }
  3845. void TextEdit::begin_complex_operation() {
  3846. _push_current_op();
  3847. next_operation_is_complex = true;
  3848. }
  3849. void TextEdit::end_complex_operation() {
  3850. _push_current_op();
  3851. ERR_FAIL_COND(undo_stack.size() == 0);
  3852. if (undo_stack.back()->get().chain_forward) {
  3853. undo_stack.back()->get().chain_forward = false;
  3854. return;
  3855. }
  3856. undo_stack.back()->get().chain_backward = true;
  3857. }
  3858. void TextEdit::_push_current_op() {
  3859. if (current_op.type == TextOperation::TYPE_NONE)
  3860. return; // do nothing
  3861. if (next_operation_is_complex) {
  3862. current_op.chain_forward = true;
  3863. next_operation_is_complex = false;
  3864. }
  3865. undo_stack.push_back(current_op);
  3866. current_op.type = TextOperation::TYPE_NONE;
  3867. current_op.text = "";
  3868. current_op.chain_forward = false;
  3869. }
  3870. void TextEdit::set_indent_using_spaces(const bool p_use_spaces) {
  3871. indent_using_spaces = p_use_spaces;
  3872. }
  3873. bool TextEdit::is_indent_using_spaces() const {
  3874. return indent_using_spaces;
  3875. }
  3876. void TextEdit::set_indent_size(const int p_size) {
  3877. ERR_FAIL_COND(p_size <= 0);
  3878. indent_size = p_size;
  3879. text.set_indent_size(p_size);
  3880. space_indent = "";
  3881. for (int i = 0; i < p_size; i++) {
  3882. space_indent += " ";
  3883. }
  3884. update();
  3885. }
  3886. void TextEdit::set_draw_tabs(bool p_draw) {
  3887. draw_tabs = p_draw;
  3888. }
  3889. bool TextEdit::is_drawing_tabs() const {
  3890. return draw_tabs;
  3891. }
  3892. void TextEdit::set_override_selected_font_color(bool p_override_selected_font_color) {
  3893. override_selected_font_color = p_override_selected_font_color;
  3894. }
  3895. bool TextEdit::is_overriding_selected_font_color() const {
  3896. return override_selected_font_color;
  3897. }
  3898. void TextEdit::set_insert_mode(bool p_enabled) {
  3899. insert_mode = p_enabled;
  3900. update();
  3901. }
  3902. bool TextEdit::is_insert_mode() const {
  3903. return insert_mode;
  3904. }
  3905. bool TextEdit::is_insert_text_operation() {
  3906. return (current_op.type == TextOperation::TYPE_INSERT);
  3907. }
  3908. uint32_t TextEdit::get_version() const {
  3909. return current_op.version;
  3910. }
  3911. uint32_t TextEdit::get_saved_version() const {
  3912. return saved_version;
  3913. }
  3914. void TextEdit::tag_saved_version() {
  3915. saved_version = get_version();
  3916. }
  3917. int TextEdit::get_v_scroll() const {
  3918. return v_scroll->get_value();
  3919. }
  3920. void TextEdit::set_v_scroll(int p_scroll) {
  3921. if (p_scroll < 0) {
  3922. p_scroll = 0;
  3923. }
  3924. if (!scroll_past_end_of_file_enabled) {
  3925. if (p_scroll + get_visible_rows() > get_total_unhidden_rows()) {
  3926. int num_rows = num_lines_from(CLAMP(p_scroll, 0, text.size() - 1), MIN(get_visible_rows(), text.size() - 1 - p_scroll));
  3927. p_scroll = text.size() - num_rows;
  3928. }
  3929. }
  3930. v_scroll->set_value(p_scroll);
  3931. cursor.line_ofs = num_lines_from(0, p_scroll);
  3932. line_scroll_pos = p_scroll;
  3933. }
  3934. int TextEdit::get_h_scroll() const {
  3935. return h_scroll->get_value();
  3936. }
  3937. void TextEdit::set_h_scroll(int p_scroll) {
  3938. h_scroll->set_value(p_scroll);
  3939. }
  3940. void TextEdit::set_smooth_scroll_enabled(bool p_enable) {
  3941. v_scroll->set_smooth_scroll_enabled(p_enable);
  3942. smooth_scroll_enabled = p_enable;
  3943. }
  3944. bool TextEdit::is_smooth_scroll_enabled() const {
  3945. return smooth_scroll_enabled;
  3946. }
  3947. void TextEdit::set_v_scroll_speed(float p_speed) {
  3948. v_scroll_speed = p_speed;
  3949. }
  3950. float TextEdit::get_v_scroll_speed() const {
  3951. return v_scroll_speed;
  3952. }
  3953. void TextEdit::set_completion(bool p_enabled, const Vector<String> &p_prefixes) {
  3954. completion_prefixes.clear();
  3955. completion_enabled = p_enabled;
  3956. for (int i = 0; i < p_prefixes.size(); i++)
  3957. completion_prefixes.insert(p_prefixes[i]);
  3958. }
  3959. void TextEdit::_confirm_completion() {
  3960. begin_complex_operation();
  3961. _remove_text(cursor.line, cursor.column - completion_base.length(), cursor.line, cursor.column);
  3962. cursor_set_column(cursor.column - completion_base.length(), false);
  3963. insert_text_at_cursor(completion_current);
  3964. if (completion_current.ends_with("(") && auto_brace_completion_enabled) {
  3965. insert_text_at_cursor(")");
  3966. cursor.column--;
  3967. }
  3968. end_complex_operation();
  3969. _cancel_completion();
  3970. }
  3971. void TextEdit::_cancel_code_hint() {
  3972. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
  3973. raised_from_completion = false;
  3974. completion_hint = "";
  3975. update();
  3976. }
  3977. void TextEdit::_cancel_completion() {
  3978. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
  3979. raised_from_completion = false;
  3980. if (!completion_active)
  3981. return;
  3982. completion_active = false;
  3983. completion_forced = false;
  3984. update();
  3985. }
  3986. static bool _is_completable(CharType c) {
  3987. return !_is_symbol(c) || c == '"' || c == '\'';
  3988. }
  3989. void TextEdit::_update_completion_candidates() {
  3990. String l = text[cursor.line];
  3991. int cofs = CLAMP(cursor.column, 0, l.length());
  3992. String s;
  3993. //look for keywords first
  3994. bool inquote = false;
  3995. int first_quote = -1;
  3996. int c = cofs - 1;
  3997. while (c >= 0) {
  3998. if (l[c] == '"' || l[c] == '\'') {
  3999. inquote = !inquote;
  4000. if (first_quote == -1)
  4001. first_quote = c;
  4002. }
  4003. c--;
  4004. }
  4005. bool pre_keyword = false;
  4006. bool cancel = false;
  4007. //print_line("inquote: "+itos(inquote)+"first quote "+itos(first_quote)+" cofs-1 "+itos(cofs-1));
  4008. if (!inquote && first_quote == cofs - 1) {
  4009. //no completion here
  4010. //print_line("cancel!");
  4011. cancel = true;
  4012. } else if (inquote && first_quote != -1) {
  4013. s = l.substr(first_quote, cofs - first_quote);
  4014. //print_line("s: 1"+s);
  4015. } else if (cofs > 0 && l[cofs - 1] == ' ') {
  4016. int kofs = cofs - 1;
  4017. String kw;
  4018. while (kofs >= 0 && l[kofs] == ' ')
  4019. kofs--;
  4020. while (kofs >= 0 && l[kofs] > 32 && _is_completable(l[kofs])) {
  4021. kw = String::chr(l[kofs]) + kw;
  4022. kofs--;
  4023. }
  4024. pre_keyword = keywords.has(kw);
  4025. //print_line("KW "+kw+"? "+itos(pre_keyword));
  4026. } else {
  4027. while (cofs > 0 && l[cofs - 1] > 32 && _is_completable(l[cofs - 1])) {
  4028. s = String::chr(l[cofs - 1]) + s;
  4029. if (l[cofs - 1] == '\'' || l[cofs - 1] == '"' || l[cofs - 1] == '$')
  4030. break;
  4031. cofs--;
  4032. }
  4033. }
  4034. if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_forced) {
  4035. cancel = true;
  4036. }
  4037. update();
  4038. bool prev_is_prefix = false;
  4039. if (cofs > 0 && completion_prefixes.has(String::chr(l[cofs - 1])))
  4040. prev_is_prefix = true;
  4041. if (cofs > 1 && l[cofs - 1] == ' ' && completion_prefixes.has(String::chr(l[cofs - 2]))) //check with one space before prefix, to allow indent
  4042. prev_is_prefix = true;
  4043. if (cancel || (!pre_keyword && s == "" && (cofs == 0 || !prev_is_prefix))) {
  4044. //none to complete, cancel
  4045. _cancel_completion();
  4046. return;
  4047. }
  4048. completion_options.clear();
  4049. completion_index = 0;
  4050. completion_base = s;
  4051. Vector<float> sim_cache;
  4052. bool single_quote = s.begins_with("'");
  4053. for (int i = 0; i < completion_strings.size(); i++) {
  4054. if (single_quote && completion_strings[i].is_quoted()) {
  4055. completion_strings[i] = completion_strings[i].unquote().quote("'");
  4056. }
  4057. if (s == completion_strings[i]) {
  4058. // A perfect match, stop completion
  4059. _cancel_completion();
  4060. return;
  4061. }
  4062. if (s.is_subsequence_ofi(completion_strings[i])) {
  4063. // don't remove duplicates if no input is provided
  4064. if (s != "" && completion_options.find(completion_strings[i]) != -1) {
  4065. continue;
  4066. }
  4067. // Calculate the similarity to keep completions in good order
  4068. float similarity;
  4069. if (completion_strings[i].to_lower().begins_with(s.to_lower())) {
  4070. // Substrings are the best candidates
  4071. similarity = 1.1;
  4072. } else {
  4073. // Otherwise compute the similarity
  4074. similarity = s.to_lower().similarity(completion_strings[i].to_lower());
  4075. }
  4076. int comp_size = completion_options.size();
  4077. if (comp_size == 0) {
  4078. completion_options.push_back(completion_strings[i]);
  4079. sim_cache.push_back(similarity);
  4080. } else {
  4081. float comp_sim;
  4082. int pos = 0;
  4083. do {
  4084. comp_sim = sim_cache[pos++];
  4085. } while (pos < comp_size && similarity < comp_sim);
  4086. pos = similarity > comp_sim ? pos - 1 : pos; // Pos will be off by one
  4087. completion_options.insert(pos, completion_strings[i]);
  4088. sim_cache.insert(pos, similarity);
  4089. }
  4090. }
  4091. }
  4092. if (completion_options.size() == 0) {
  4093. //no options to complete, cancel
  4094. _cancel_completion();
  4095. return;
  4096. }
  4097. // The top of the list is the best match
  4098. completion_current = completion_options[0];
  4099. completion_enabled = true;
  4100. }
  4101. void TextEdit::query_code_comple() {
  4102. String l = text[cursor.line];
  4103. int ofs = CLAMP(cursor.column, 0, l.length());
  4104. bool inquote = false;
  4105. int c = ofs - 1;
  4106. while (c >= 0) {
  4107. if (l[c] == '"' || l[c] == '\'')
  4108. inquote = !inquote;
  4109. c--;
  4110. }
  4111. if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1]))))
  4112. emit_signal("request_completion");
  4113. 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
  4114. emit_signal("request_completion");
  4115. }
  4116. void TextEdit::set_code_hint(const String &p_hint) {
  4117. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
  4118. raised_from_completion = true;
  4119. completion_hint = p_hint;
  4120. completion_hint_offset = -0xFFFF;
  4121. update();
  4122. }
  4123. void TextEdit::code_complete(const Vector<String> &p_strings, bool p_forced) {
  4124. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
  4125. raised_from_completion = true;
  4126. completion_strings = p_strings;
  4127. completion_active = true;
  4128. completion_forced = p_forced;
  4129. completion_current = "";
  4130. completion_index = 0;
  4131. _update_completion_candidates();
  4132. //
  4133. }
  4134. String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
  4135. int row, col;
  4136. _get_mouse_pos(p_pos, row, col);
  4137. String s = text[row];
  4138. if (s.length() == 0)
  4139. return "";
  4140. int beg = CLAMP(col, 0, s.length());
  4141. int end = beg;
  4142. if (s[beg] > 32 || beg == s.length()) {
  4143. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  4144. bool inside_quotes = false;
  4145. int qbegin = 0, qend = 0;
  4146. for (int i = 0; i < s.length(); i++) {
  4147. if (s[i] == '"') {
  4148. if (inside_quotes) {
  4149. qend = i;
  4150. inside_quotes = false;
  4151. if (col >= qbegin && col <= qend) {
  4152. return s.substr(qbegin, qend - qbegin);
  4153. }
  4154. } else {
  4155. qbegin = i + 1;
  4156. inside_quotes = true;
  4157. }
  4158. }
  4159. }
  4160. while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) {
  4161. beg--;
  4162. }
  4163. while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) {
  4164. end++;
  4165. }
  4166. if (end < s.length())
  4167. end += 1;
  4168. return s.substr(beg, end - beg);
  4169. }
  4170. return String();
  4171. }
  4172. String TextEdit::get_tooltip(const Point2 &p_pos) const {
  4173. if (!tooltip_obj)
  4174. return Control::get_tooltip(p_pos);
  4175. int row, col;
  4176. _get_mouse_pos(p_pos, row, col);
  4177. String s = text[row];
  4178. if (s.length() == 0)
  4179. return Control::get_tooltip(p_pos);
  4180. int beg = CLAMP(col, 0, s.length());
  4181. int end = beg;
  4182. if (s[beg] > 32 || beg == s.length()) {
  4183. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  4184. while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) {
  4185. beg--;
  4186. }
  4187. while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) {
  4188. end++;
  4189. }
  4190. if (end < s.length())
  4191. end += 1;
  4192. String tt = tooltip_obj->call(tooltip_func, s.substr(beg, end - beg), tooltip_ud);
  4193. return tt;
  4194. }
  4195. return Control::get_tooltip(p_pos);
  4196. }
  4197. void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata) {
  4198. tooltip_obj = p_obj;
  4199. tooltip_func = p_function;
  4200. tooltip_ud = p_udata;
  4201. }
  4202. void TextEdit::set_line(int line, String new_text) {
  4203. if (line < 0 || line > text.size())
  4204. return;
  4205. _remove_text(line, 0, line, text[line].length());
  4206. _insert_text(line, 0, new_text);
  4207. if (cursor.line == line) {
  4208. cursor.column = MIN(cursor.column, new_text.length());
  4209. }
  4210. }
  4211. void TextEdit::insert_at(const String &p_text, int at) {
  4212. cursor_set_column(0);
  4213. cursor_set_line(at, false, true);
  4214. _insert_text(at, 0, p_text + "\n");
  4215. }
  4216. void TextEdit::set_show_line_numbers(bool p_show) {
  4217. line_numbers = p_show;
  4218. update();
  4219. }
  4220. void TextEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  4221. line_numbers_zero_padded = p_zero_padded;
  4222. update();
  4223. }
  4224. bool TextEdit::is_show_line_numbers_enabled() const {
  4225. return line_numbers;
  4226. }
  4227. void TextEdit::set_show_line_length_guideline(bool p_show) {
  4228. line_length_guideline = p_show;
  4229. update();
  4230. }
  4231. void TextEdit::set_line_length_guideline_column(int p_column) {
  4232. line_length_guideline_col = p_column;
  4233. update();
  4234. }
  4235. void TextEdit::set_draw_breakpoint_gutter(bool p_draw) {
  4236. draw_breakpoint_gutter = p_draw;
  4237. update();
  4238. }
  4239. bool TextEdit::is_drawing_breakpoint_gutter() const {
  4240. return draw_breakpoint_gutter;
  4241. }
  4242. void TextEdit::set_breakpoint_gutter_width(int p_gutter_width) {
  4243. breakpoint_gutter_width = p_gutter_width;
  4244. update();
  4245. }
  4246. int TextEdit::get_breakpoint_gutter_width() const {
  4247. return cache.breakpoint_gutter_width;
  4248. }
  4249. void TextEdit::set_draw_fold_gutter(bool p_draw) {
  4250. draw_fold_gutter = p_draw;
  4251. update();
  4252. }
  4253. bool TextEdit::is_drawing_fold_gutter() const {
  4254. return draw_fold_gutter;
  4255. }
  4256. void TextEdit::set_fold_gutter_width(int p_gutter_width) {
  4257. fold_gutter_width = p_gutter_width;
  4258. update();
  4259. }
  4260. int TextEdit::get_fold_gutter_width() const {
  4261. return cache.fold_gutter_width;
  4262. }
  4263. void TextEdit::set_hiding_enabled(int p_enabled) {
  4264. if (!p_enabled)
  4265. unhide_all_lines();
  4266. hiding_enabled = p_enabled;
  4267. update();
  4268. }
  4269. int TextEdit::is_hiding_enabled() const {
  4270. return hiding_enabled;
  4271. }
  4272. void TextEdit::set_highlight_current_line(bool p_enabled) {
  4273. highlight_current_line = p_enabled;
  4274. update();
  4275. }
  4276. bool TextEdit::is_highlight_current_line_enabled() const {
  4277. return highlight_current_line;
  4278. }
  4279. bool TextEdit::is_text_field() const {
  4280. return true;
  4281. }
  4282. void TextEdit::menu_option(int p_option) {
  4283. switch (p_option) {
  4284. case MENU_CUT: {
  4285. if (!readonly) {
  4286. cut();
  4287. }
  4288. } break;
  4289. case MENU_COPY: {
  4290. copy();
  4291. } break;
  4292. case MENU_PASTE: {
  4293. if (!readonly) {
  4294. paste();
  4295. }
  4296. } break;
  4297. case MENU_CLEAR: {
  4298. if (!readonly) {
  4299. clear();
  4300. }
  4301. } break;
  4302. case MENU_SELECT_ALL: {
  4303. select_all();
  4304. } break;
  4305. case MENU_UNDO: {
  4306. undo();
  4307. } break;
  4308. };
  4309. }
  4310. void TextEdit::set_select_identifiers_on_hover(bool p_enable) {
  4311. select_identifiers_enabled = p_enable;
  4312. }
  4313. bool TextEdit::is_selecting_identifiers_on_hover_enabled() const {
  4314. return select_identifiers_enabled;
  4315. }
  4316. void TextEdit::set_context_menu_enabled(bool p_enable) {
  4317. context_menu_enabled = p_enable;
  4318. }
  4319. bool TextEdit::is_context_menu_enabled() {
  4320. return context_menu_enabled;
  4321. }
  4322. PopupMenu *TextEdit::get_menu() const {
  4323. return menu;
  4324. }
  4325. void TextEdit::_bind_methods() {
  4326. ClassDB::bind_method(D_METHOD("_gui_input"), &TextEdit::_gui_input);
  4327. ClassDB::bind_method(D_METHOD("_scroll_moved"), &TextEdit::_scroll_moved);
  4328. ClassDB::bind_method(D_METHOD("_cursor_changed_emit"), &TextEdit::_cursor_changed_emit);
  4329. ClassDB::bind_method(D_METHOD("_text_changed_emit"), &TextEdit::_text_changed_emit);
  4330. ClassDB::bind_method(D_METHOD("_push_current_op"), &TextEdit::_push_current_op);
  4331. ClassDB::bind_method(D_METHOD("_click_selection_held"), &TextEdit::_click_selection_held);
  4332. ClassDB::bind_method(D_METHOD("_toggle_draw_caret"), &TextEdit::_toggle_draw_caret);
  4333. ClassDB::bind_method(D_METHOD("_v_scroll_input"), &TextEdit::_v_scroll_input);
  4334. BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE);
  4335. BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS);
  4336. BIND_ENUM_CONSTANT(SEARCH_BACKWARDS);
  4337. /*
  4338. ClassDB::bind_method(D_METHOD("delete_char"),&TextEdit::delete_char);
  4339. ClassDB::bind_method(D_METHOD("delete_line"),&TextEdit::delete_line);
  4340. */
  4341. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text);
  4342. ClassDB::bind_method(D_METHOD("insert_text_at_cursor", "text"), &TextEdit::insert_text_at_cursor);
  4343. ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count);
  4344. ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
  4345. ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
  4346. ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(true));
  4347. ClassDB::bind_method(D_METHOD("cursor_set_line", "line", "adjust_viewport", "can_be_hidden"), &TextEdit::cursor_set_line, DEFVAL(true), DEFVAL(true));
  4348. ClassDB::bind_method(D_METHOD("cursor_get_column"), &TextEdit::cursor_get_column);
  4349. ClassDB::bind_method(D_METHOD("cursor_get_line"), &TextEdit::cursor_get_line);
  4350. ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enable"), &TextEdit::cursor_set_blink_enabled);
  4351. ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &TextEdit::cursor_get_blink_enabled);
  4352. ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &TextEdit::cursor_set_blink_speed);
  4353. ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &TextEdit::cursor_get_blink_speed);
  4354. ClassDB::bind_method(D_METHOD("cursor_set_block_mode", "enable"), &TextEdit::cursor_set_block_mode);
  4355. ClassDB::bind_method(D_METHOD("cursor_is_block_mode"), &TextEdit::cursor_is_block_mode);
  4356. ClassDB::bind_method(D_METHOD("set_right_click_moves_caret", "enable"), &TextEdit::set_right_click_moves_caret);
  4357. ClassDB::bind_method(D_METHOD("is_right_click_moving_caret"), &TextEdit::is_right_click_moving_caret);
  4358. ClassDB::bind_method(D_METHOD("set_readonly", "enable"), &TextEdit::set_readonly);
  4359. ClassDB::bind_method(D_METHOD("is_readonly"), &TextEdit::is_readonly);
  4360. ClassDB::bind_method(D_METHOD("set_wrap", "enable"), &TextEdit::set_wrap);
  4361. ClassDB::bind_method(D_METHOD("set_max_chars", "amount"), &TextEdit::set_max_chars);
  4362. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &TextEdit::set_context_menu_enabled);
  4363. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled);
  4364. ClassDB::bind_method(D_METHOD("cut"), &TextEdit::cut);
  4365. ClassDB::bind_method(D_METHOD("copy"), &TextEdit::copy);
  4366. ClassDB::bind_method(D_METHOD("paste"), &TextEdit::paste);
  4367. ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column"), &TextEdit::select);
  4368. ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
  4369. ClassDB::bind_method(D_METHOD("deselect"), &TextEdit::deselect);
  4370. ClassDB::bind_method(D_METHOD("is_selection_active"), &TextEdit::is_selection_active);
  4371. ClassDB::bind_method(D_METHOD("get_selection_from_line"), &TextEdit::get_selection_from_line);
  4372. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &TextEdit::get_selection_from_column);
  4373. ClassDB::bind_method(D_METHOD("get_selection_to_line"), &TextEdit::get_selection_to_line);
  4374. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &TextEdit::get_selection_to_column);
  4375. ClassDB::bind_method(D_METHOD("get_selection_text"), &TextEdit::get_selection_text);
  4376. ClassDB::bind_method(D_METHOD("get_word_under_cursor"), &TextEdit::get_word_under_cursor);
  4377. ClassDB::bind_method(D_METHOD("search", "key", "flags", "from_line", "from_column"), &TextEdit::_search_bind);
  4378. ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo);
  4379. ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo);
  4380. ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history);
  4381. ClassDB::bind_method(D_METHOD("set_show_line_numbers", "enable"), &TextEdit::set_show_line_numbers);
  4382. ClassDB::bind_method(D_METHOD("is_show_line_numbers_enabled"), &TextEdit::is_show_line_numbers_enabled);
  4383. ClassDB::bind_method(D_METHOD("set_hiding_enabled", "enable"), &TextEdit::set_hiding_enabled);
  4384. ClassDB::bind_method(D_METHOD("is_hiding_enabled"), &TextEdit::is_hiding_enabled);
  4385. ClassDB::bind_method(D_METHOD("set_line_as_hidden", "line", "enable"), &TextEdit::set_line_as_hidden);
  4386. ClassDB::bind_method(D_METHOD("is_line_hidden"), &TextEdit::is_line_hidden);
  4387. ClassDB::bind_method(D_METHOD("fold_all_lines"), &TextEdit::fold_all_lines);
  4388. ClassDB::bind_method(D_METHOD("unhide_all_lines"), &TextEdit::unhide_all_lines);
  4389. ClassDB::bind_method(D_METHOD("fold_line", "line"), &TextEdit::fold_line);
  4390. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &TextEdit::unfold_line);
  4391. ClassDB::bind_method(D_METHOD("toggle_fold_line", "line"), &TextEdit::toggle_fold_line);
  4392. ClassDB::bind_method(D_METHOD("can_fold", "line"), &TextEdit::can_fold);
  4393. ClassDB::bind_method(D_METHOD("is_folded", "line"), &TextEdit::is_folded);
  4394. ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enable"), &TextEdit::set_highlight_all_occurrences);
  4395. ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  4396. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &TextEdit::set_override_selected_font_color);
  4397. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &TextEdit::is_overriding_selected_font_color);
  4398. ClassDB::bind_method(D_METHOD("set_syntax_coloring", "enable"), &TextEdit::set_syntax_coloring);
  4399. ClassDB::bind_method(D_METHOD("is_syntax_coloring_enabled"), &TextEdit::is_syntax_coloring_enabled);
  4400. ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
  4401. ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
  4402. ClassDB::bind_method(D_METHOD("set_smooth_scroll_enable", "enable"), &TextEdit::set_smooth_scroll_enabled);
  4403. ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
  4404. ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
  4405. ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
  4406. ClassDB::bind_method(D_METHOD("add_keyword_color", "keyword", "color"), &TextEdit::add_keyword_color);
  4407. ClassDB::bind_method(D_METHOD("add_color_region", "begin_key", "end_key", "color", "line_only"), &TextEdit::add_color_region, DEFVAL(false));
  4408. ClassDB::bind_method(D_METHOD("clear_colors"), &TextEdit::clear_colors);
  4409. ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
  4410. ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu);
  4411. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  4412. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "readonly"), "set_readonly", "is_readonly");
  4413. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
  4414. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), "set_syntax_coloring", "is_syntax_coloring_enabled");
  4415. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), "set_show_line_numbers", "is_show_line_numbers_enabled");
  4416. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled");
  4417. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  4418. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  4419. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_scrolling"), "set_smooth_scroll_enable", "is_smooth_scroll_enabled");
  4420. ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_scroll_speed"), "set_v_scroll_speed", "get_v_scroll_speed");
  4421. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hiding_enabled"), "set_hiding_enabled", "is_hiding_enabled");
  4422. ADD_GROUP("Caret", "caret_");
  4423. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_block_mode"), "cursor_set_block_mode", "cursor_is_block_mode");
  4424. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  4425. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.1"), "cursor_set_blink_speed", "cursor_get_blink_speed");
  4426. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_moving_by_right_click"), "set_right_click_moves_caret", "is_right_click_moving_caret");
  4427. ADD_SIGNAL(MethodInfo("cursor_changed"));
  4428. ADD_SIGNAL(MethodInfo("text_changed"));
  4429. ADD_SIGNAL(MethodInfo("request_completion"));
  4430. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "row")));
  4431. ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "row"), PropertyInfo(Variant::INT, "column")));
  4432. BIND_ENUM_CONSTANT(MENU_CUT);
  4433. BIND_ENUM_CONSTANT(MENU_COPY);
  4434. BIND_ENUM_CONSTANT(MENU_PASTE);
  4435. BIND_ENUM_CONSTANT(MENU_CLEAR);
  4436. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  4437. BIND_ENUM_CONSTANT(MENU_UNDO);
  4438. BIND_ENUM_CONSTANT(MENU_MAX);
  4439. GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3);
  4440. }
  4441. TextEdit::TextEdit() {
  4442. readonly = false;
  4443. setting_row = false;
  4444. draw_tabs = false;
  4445. override_selected_font_color = false;
  4446. draw_caret = true;
  4447. max_chars = 0;
  4448. clear();
  4449. wrap = false;
  4450. set_focus_mode(FOCUS_ALL);
  4451. _update_caches();
  4452. cache.size = Size2(1, 1);
  4453. cache.row_height = 1;
  4454. cache.line_spacing = 1;
  4455. cache.line_number_w = 1;
  4456. cache.breakpoint_gutter_width = 0;
  4457. breakpoint_gutter_width = 0;
  4458. cache.fold_gutter_width = 0;
  4459. fold_gutter_width = 0;
  4460. indent_size = 4;
  4461. text.set_indent_size(indent_size);
  4462. text.clear();
  4463. //text.insert(1,"Mongolia..");
  4464. //text.insert(2,"PAIS GENEROSO!!");
  4465. text.set_color_regions(&color_regions);
  4466. h_scroll = memnew(HScrollBar);
  4467. v_scroll = memnew(VScrollBar);
  4468. add_child(h_scroll);
  4469. add_child(v_scroll);
  4470. updating_scrolls = false;
  4471. selection.active = false;
  4472. h_scroll->connect("value_changed", this, "_scroll_moved");
  4473. v_scroll->connect("value_changed", this, "_scroll_moved");
  4474. v_scroll->connect("scrolling", this, "_v_scroll_input");
  4475. cursor_changed_dirty = false;
  4476. text_changed_dirty = false;
  4477. selection.selecting_mode = Selection::MODE_NONE;
  4478. selection.selecting_line = 0;
  4479. selection.selecting_column = 0;
  4480. selection.selecting_text = false;
  4481. selection.active = false;
  4482. syntax_coloring = false;
  4483. block_caret = false;
  4484. caret_blink_enabled = false;
  4485. caret_blink_timer = memnew(Timer);
  4486. add_child(caret_blink_timer);
  4487. caret_blink_timer->set_wait_time(0.65);
  4488. caret_blink_timer->connect("timeout", this, "_toggle_draw_caret");
  4489. cursor_set_blink_enabled(false);
  4490. right_click_moves_caret = true;
  4491. idle_detect = memnew(Timer);
  4492. add_child(idle_detect);
  4493. idle_detect->set_one_shot(true);
  4494. idle_detect->set_wait_time(GLOBAL_GET("gui/timers/text_edit_idle_detect_sec"));
  4495. idle_detect->connect("timeout", this, "_push_current_op");
  4496. click_select_held = memnew(Timer);
  4497. add_child(click_select_held);
  4498. click_select_held->set_wait_time(0.05);
  4499. click_select_held->connect("timeout", this, "_click_selection_held");
  4500. current_op.type = TextOperation::TYPE_NONE;
  4501. undo_enabled = true;
  4502. undo_stack_pos = NULL;
  4503. setting_text = false;
  4504. last_dblclk = 0;
  4505. current_op.version = 0;
  4506. version = 0;
  4507. saved_version = 0;
  4508. completion_enabled = false;
  4509. completion_active = false;
  4510. completion_line_ofs = 0;
  4511. tooltip_obj = NULL;
  4512. line_numbers = false;
  4513. line_numbers_zero_padded = false;
  4514. line_length_guideline = false;
  4515. line_length_guideline_col = 80;
  4516. draw_breakpoint_gutter = false;
  4517. draw_fold_gutter = false;
  4518. hiding_enabled = false;
  4519. next_operation_is_complex = false;
  4520. scroll_past_end_of_file_enabled = false;
  4521. auto_brace_completion_enabled = false;
  4522. brace_matching_enabled = false;
  4523. highlight_all_occurrences = false;
  4524. highlight_current_line = false;
  4525. indent_using_spaces = false;
  4526. space_indent = " ";
  4527. auto_indent = false;
  4528. insert_mode = false;
  4529. window_has_focus = true;
  4530. select_identifiers_enabled = false;
  4531. smooth_scroll_enabled = false;
  4532. scrolling = false;
  4533. target_v_scroll = 0;
  4534. v_scroll_speed = 80;
  4535. raised_from_completion = false;
  4536. context_menu_enabled = true;
  4537. menu = memnew(PopupMenu);
  4538. add_child(menu);
  4539. menu->add_item(TTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X);
  4540. menu->add_item(TTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C);
  4541. menu->add_item(TTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V);
  4542. menu->add_separator();
  4543. menu->add_item(TTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A);
  4544. menu->add_item(TTR("Clear"), MENU_CLEAR);
  4545. menu->add_separator();
  4546. menu->add_item(TTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z);
  4547. menu->connect("id_pressed", this, "menu_option");
  4548. }
  4549. TextEdit::~TextEdit() {
  4550. }