text_edit.cpp 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827
  1. /*************************************************************************/
  2. /* text_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "text_edit.h"
  30. #include "os/keyboard.h"
  31. #include "os/input.h"
  32. #include "os/os.h"
  33. #include "globals.h"
  34. #include "message_queue.h"
  35. #include "scene/main/viewport.h"
  36. #define TAB_PIXELS
  37. static bool _is_text_char(CharType c) {
  38. return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_';
  39. }
  40. static bool _is_symbol(CharType c) {
  41. return c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t' || c==' ');
  42. }
  43. static bool _is_char(CharType c) {
  44. return (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_';
  45. }
  46. static bool _is_number(CharType c) {
  47. return (c >= '0' && c <= '9');
  48. }
  49. static bool _is_hex_symbol(CharType c) {
  50. return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  51. }
  52. static bool _is_pair_right_symbol(CharType c) {
  53. return
  54. c == '"' ||
  55. c == '\'' ||
  56. c == ')' ||
  57. c == ']' ||
  58. c == '}';
  59. }
  60. static bool _is_pair_left_symbol(CharType c) {
  61. return
  62. c == '"' ||
  63. c == '\'' ||
  64. c == '(' ||
  65. c == '[' ||
  66. c == '{';
  67. }
  68. static bool _is_pair_symbol(CharType c) {
  69. return _is_pair_left_symbol(c) || _is_pair_right_symbol(c);
  70. }
  71. static CharType _get_right_pair_symbol(CharType c) {
  72. if(c == '"')
  73. return '"';
  74. if(c == '\'')
  75. return '\'';
  76. if(c == '(')
  77. return ')';
  78. if(c == '[')
  79. return ']';
  80. if(c == '{')
  81. return '}';
  82. return 0;
  83. }
  84. void TextEdit::Text::set_font(const Ref<Font>& p_font) {
  85. font=p_font;
  86. }
  87. void TextEdit::Text::set_tab_size(int p_tab_size) {
  88. tab_size=p_tab_size;
  89. }
  90. void TextEdit::Text::_update_line_cache(int p_line) const {
  91. int w = 0;
  92. int tab_w=font->get_char_size(' ').width*tab_size;
  93. int len = text[p_line].data.length();
  94. const CharType *str = text[p_line].data.c_str();
  95. //update width
  96. for(int i=0;i<len;i++) {
  97. if (str[i]=='\t') {
  98. int left = w%tab_w;
  99. if (left==0)
  100. w+=tab_w;
  101. else
  102. w+=tab_w-w%tab_w; // is right...
  103. } else {
  104. w+=font->get_char_size(str[i],str[i+1]).width;
  105. }
  106. }
  107. text[p_line].width_cache=w;
  108. //update regions
  109. text[p_line].region_info.clear();
  110. for(int i=0;i<len;i++) {
  111. if (!_is_symbol(str[i]))
  112. continue;
  113. if (str[i]=='\\') {
  114. i++; //skip quoted anything
  115. continue;
  116. }
  117. int left=len-i;
  118. for(int j=0;j<color_regions->size();j++) {
  119. const ColorRegion& cr=color_regions->operator [](j);
  120. /* BEGIN */
  121. int lr=cr.begin_key.length();
  122. if (lr==0 || lr>left)
  123. continue;
  124. const CharType* kc = cr.begin_key.c_str();
  125. bool match=true;
  126. for(int k=0;k<lr;k++) {
  127. if (kc[k]!=str[i+k]) {
  128. match=false;
  129. break;
  130. }
  131. }
  132. if (match) {
  133. ColorRegionInfo cri;
  134. cri.end=false;
  135. cri.region=j;
  136. text[p_line].region_info[i]=cri;
  137. i+=lr-1;
  138. break;
  139. }
  140. /* END */
  141. lr=cr.end_key.length();
  142. if (lr==0 || lr>left)
  143. continue;
  144. kc = cr.end_key.c_str();
  145. match=true;
  146. for(int k=0;k<lr;k++) {
  147. if (kc[k]!=str[i+k]) {
  148. match=false;
  149. break;
  150. }
  151. }
  152. if (match) {
  153. ColorRegionInfo cri;
  154. cri.end=true;
  155. cri.region=j;
  156. text[p_line].region_info[i]=cri;
  157. i+=lr-1;
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. const Map<int,TextEdit::Text::ColorRegionInfo>& TextEdit::Text::get_color_region_info(int p_line) {
  164. Map<int,ColorRegionInfo> *cri=NULL;
  165. ERR_FAIL_INDEX_V(p_line,text.size(),*cri); //enjoy your crash
  166. if (text[p_line].width_cache==-1) {
  167. _update_line_cache(p_line);
  168. }
  169. return text[p_line].region_info;
  170. }
  171. int TextEdit::Text::get_line_width(int p_line) const {
  172. ERR_FAIL_INDEX_V(p_line,text.size(),-1);
  173. if (text[p_line].width_cache==-1) {
  174. _update_line_cache(p_line);
  175. }
  176. return text[p_line].width_cache;
  177. }
  178. void TextEdit::Text::clear_caches() {
  179. for(int i=0;i<text.size();i++)
  180. text[i].width_cache=-1;
  181. }
  182. void TextEdit::Text::clear() {
  183. text.clear();;
  184. insert(0,"");
  185. }
  186. int TextEdit::Text::get_max_width() const {
  187. //quite some work.. but should be fast enough.
  188. int max = 0;
  189. for(int i=0;i<text.size();i++)
  190. max=MAX(max,get_line_width(i));
  191. return max;
  192. }
  193. void TextEdit::Text::set(int p_line,const String& p_text) {
  194. ERR_FAIL_INDEX(p_line,text.size());
  195. text[p_line].width_cache=-1;
  196. text[p_line].data=p_text;
  197. }
  198. void TextEdit::Text::insert(int p_at,const String& p_text) {
  199. Line line;
  200. line.marked=false;
  201. line.breakpoint=false;
  202. line.width_cache=-1;
  203. line.data=p_text;
  204. text.insert(p_at,line);
  205. }
  206. void TextEdit::Text::remove(int p_at) {
  207. text.remove(p_at);
  208. }
  209. void TextEdit::_update_scrollbars() {
  210. Size2 size = get_size();
  211. Size2 hmin = h_scroll->get_combined_minimum_size();
  212. Size2 vmin = v_scroll->get_combined_minimum_size();
  213. v_scroll->set_begin( Point2(size.width - vmin.width, cache.style_normal->get_margin(MARGIN_TOP)) );
  214. v_scroll->set_end( Point2(size.width, size.height - cache.style_normal->get_margin(MARGIN_TOP) - cache.style_normal->get_margin(MARGIN_BOTTOM)) );
  215. h_scroll->set_begin( Point2( 0, size.height - hmin.height) );
  216. h_scroll->set_end( Point2(size.width-vmin.width, size.height) );
  217. int hscroll_rows = ((hmin.height-1)/get_row_height())+1;
  218. int visible_rows = get_visible_rows();
  219. int total_rows = text.size();
  220. if (scroll_past_end_of_file_enabled) {
  221. total_rows += get_visible_rows() - 1;
  222. }
  223. int vscroll_pixels = v_scroll->get_combined_minimum_size().width;
  224. int visible_width = size.width - cache.style_normal->get_minimum_size().width;
  225. int total_width = text.get_max_width() + vmin.x;
  226. if (line_numbers)
  227. total_width += cache.line_number_w;
  228. if (draw_breakpoint_gutter) {
  229. total_width += cache.breakpoint_gutter_width;
  230. }
  231. bool use_hscroll=true;
  232. bool use_vscroll=true;
  233. if (total_rows <= visible_rows && total_width <= visible_width) {
  234. //thanks yessopie for this clever bit of logic
  235. use_hscroll=false;
  236. use_vscroll=false;
  237. } else {
  238. if (total_rows > visible_rows && total_width <= visible_width - vscroll_pixels) {
  239. //thanks yessopie for this clever bit of logic
  240. use_hscroll=false;
  241. }
  242. if (total_rows <= visible_rows - hscroll_rows && total_width > visible_width) {
  243. //thanks yessopie for this clever bit of logic
  244. use_vscroll=false;
  245. }
  246. }
  247. updating_scrolls=true;
  248. if (use_vscroll) {
  249. v_scroll->show();
  250. v_scroll->set_max(total_rows);
  251. v_scroll->set_page(visible_rows);
  252. v_scroll->set_val(cursor.line_ofs);
  253. } else {
  254. cursor.line_ofs = 0;
  255. v_scroll->hide();
  256. }
  257. if (use_hscroll) {
  258. h_scroll->show();
  259. h_scroll->set_max(total_width);
  260. h_scroll->set_page(visible_width);
  261. h_scroll->set_val(cursor.x_ofs);
  262. } else {
  263. h_scroll->hide();
  264. }
  265. updating_scrolls=false;
  266. }
  267. void TextEdit::_click_selection_held() {
  268. if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && selection.selecting_mode!=Selection::MODE_NONE) {
  269. Point2 mp = Input::get_singleton()->get_mouse_pos()-get_global_pos();
  270. int row,col;
  271. _get_mouse_pos(Point2i(mp.x,mp.y), row,col);
  272. select(selection.selecting_line,selection.selecting_column,row,col);
  273. cursor_set_line( row );
  274. cursor_set_column( col );
  275. update();
  276. click_select_held->start();
  277. } else {
  278. click_select_held->stop();
  279. }
  280. }
  281. void TextEdit::_notification(int p_what) {
  282. switch(p_what) {
  283. case NOTIFICATION_ENTER_TREE: {
  284. _update_caches();
  285. if (cursor_changed_dirty)
  286. MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit");
  287. if (text_changed_dirty)
  288. MessageQueue::get_singleton()->push_call(this,"_text_changed_emit");
  289. } break;
  290. case NOTIFICATION_RESIZED: {
  291. cache.size=get_size();
  292. adjust_viewport_to_cursor();
  293. } break;
  294. case NOTIFICATION_THEME_CHANGED: {
  295. _update_caches();
  296. } break;
  297. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  298. window_has_focus = true;
  299. draw_caret = true;
  300. update();
  301. } break;
  302. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  303. window_has_focus = false;
  304. draw_caret = false;
  305. update();
  306. } break;
  307. case NOTIFICATION_DRAW: {
  308. if ((!has_focus() && !menu->has_focus()) || !window_has_focus) {
  309. draw_caret = false;
  310. }
  311. if (draw_breakpoint_gutter) {
  312. breakpoint_gutter_width = (get_row_height() * 55) / 100;
  313. cache.breakpoint_gutter_width = breakpoint_gutter_width;
  314. } else {
  315. cache.breakpoint_gutter_width = 0;
  316. }
  317. int line_number_char_count=0;
  318. {
  319. int lc=text.size()+1;
  320. cache.line_number_w=0;
  321. while(lc) {
  322. cache.line_number_w+=1;
  323. lc/=10;
  324. };
  325. if (line_numbers) {
  326. line_number_char_count=cache.line_number_w;
  327. cache.line_number_w=(cache.line_number_w+1)*cache.font->get_char_size('0').width;
  328. } else {
  329. cache.line_number_w=0;
  330. }
  331. }
  332. _update_scrollbars();
  333. RID ci = get_canvas_item();
  334. int xmargin_beg=cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width;
  335. int xmargin_end=cache.size.width-cache.style_normal->get_margin(MARGIN_RIGHT);
  336. //let's do it easy for now:
  337. cache.style_normal->draw(ci,Rect2(Point2(),cache.size));
  338. if (has_focus())
  339. cache.style_focus->draw(ci,Rect2(Point2(),cache.size));
  340. int ascent=cache.font->get_ascent();
  341. int visible_rows = get_visible_rows();
  342. int tab_w = cache.font->get_char_size(' ').width*tab_size;
  343. Color color = cache.font_color;
  344. int in_region=-1;
  345. if (syntax_coloring) {
  346. if (custom_bg_color.a>0.01) {
  347. Point2i ofs = Point2i(cache.style_normal->get_offset())/2.0;
  348. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(ofs, get_size()-cache.style_normal->get_minimum_size()+ofs),custom_bg_color);
  349. }
  350. //compute actual region to start (may be inside say, a comment).
  351. //slow in very large documments :( but ok for source!
  352. for(int i=0;i<cursor.line_ofs;i++) {
  353. const Map<int,Text::ColorRegionInfo>& cri_map=text.get_color_region_info(i);
  354. if (in_region>=0 && color_regions[in_region].line_only) {
  355. in_region=-1; //reset regions that end at end of line
  356. }
  357. for( const Map<int,Text::ColorRegionInfo>::Element* E= cri_map.front();E;E=E->next() ) {
  358. const Text::ColorRegionInfo &cri=E->get();
  359. if (in_region==-1) {
  360. if (!cri.end) {
  361. in_region=cri.region;
  362. }
  363. } else if (in_region==cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
  364. if (cri.end || color_regions[cri.region].eq) {
  365. in_region=-1;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. int brace_open_match_line=-1;
  372. int brace_open_match_column=-1;
  373. bool brace_open_matching=false;
  374. bool brace_open_mismatch=false;
  375. int brace_close_match_line=-1;
  376. int brace_close_match_column=-1;
  377. bool brace_close_matching=false;
  378. bool brace_close_mismatch=false;
  379. if (brace_matching_enabled) {
  380. if (cursor.column<text[cursor.line].length()) {
  381. //check for open
  382. CharType c = text[cursor.line][cursor.column];
  383. CharType closec=0;
  384. if (c=='[') {
  385. closec=']';
  386. } else if (c=='{') {
  387. closec='}';
  388. } else if (c=='(') {
  389. closec=')';
  390. }
  391. if (closec!=0) {
  392. int stack=1;
  393. for(int i=cursor.line;i<text.size();i++) {
  394. int from = i==cursor.line?cursor.column+1:0;
  395. for(int j=from;j<text[i].length();j++) {
  396. CharType cc = text[i][j];
  397. //ignore any brackets inside a string
  398. if (cc== '"' || cc == '\'') {
  399. CharType quotation = cc;
  400. do {
  401. j++;
  402. if (!(j<text[i].length())) {
  403. break;
  404. }
  405. cc=text[i][j];
  406. //skip over escaped quotation marks inside strings
  407. if (cc=='\\') {
  408. bool escaped = true;
  409. while (j+1<text[i].length() && text[i][j+1]=='\\') {
  410. escaped=!escaped;
  411. j++;
  412. }
  413. if (escaped) {
  414. j++;
  415. continue;
  416. }
  417. }
  418. } while (cc!= quotation);
  419. } else if (cc==c)
  420. stack++;
  421. else if (cc==closec)
  422. stack--;
  423. if (stack==0) {
  424. brace_open_match_line=i;
  425. brace_open_match_column=j;
  426. brace_open_matching=true;
  427. break;
  428. }
  429. }
  430. if (brace_open_match_line!=-1)
  431. break;
  432. }
  433. if (!brace_open_matching)
  434. brace_open_mismatch=true;
  435. }
  436. }
  437. if (cursor.column>0) {
  438. CharType c = text[cursor.line][cursor.column-1];
  439. CharType closec=0;
  440. if (c==']') {
  441. closec='[';
  442. } else if (c=='}') {
  443. closec='{';
  444. } else if (c==')') {
  445. closec='(';
  446. }
  447. if (closec!=0) {
  448. int stack=1;
  449. for(int i=cursor.line;i>=0;i--) {
  450. int from = i==cursor.line?cursor.column-2:text[i].length()-1;
  451. for(int j=from;j>=0;j--) {
  452. CharType cc = text[i][j];
  453. //ignore any brackets inside a string
  454. if (cc== '"' || cc == '\'') {
  455. CharType quotation = cc;
  456. do {
  457. j--;
  458. if (!(j>=0)) {
  459. break;
  460. }
  461. cc=text[i][j];
  462. //skip over escaped quotation marks inside strings
  463. if (cc==quotation) {
  464. bool escaped = false;
  465. while (j-1>=0 && text[i][j-1]=='\\') {
  466. escaped=!escaped;
  467. j--;
  468. }
  469. if (escaped) {
  470. j--;
  471. cc='\\';
  472. continue;
  473. }
  474. }
  475. } while (cc!= quotation);
  476. } else if (cc==c)
  477. stack++;
  478. else if (cc==closec)
  479. stack--;
  480. if (stack==0) {
  481. brace_close_match_line=i;
  482. brace_close_match_column=j;
  483. brace_close_matching=true;
  484. break;
  485. }
  486. }
  487. if (brace_close_match_line!=-1)
  488. break;
  489. }
  490. if (!brace_close_matching)
  491. brace_close_mismatch=true;
  492. }
  493. }
  494. }
  495. int deregion=0; //force it to clear inrgion
  496. Point2 cursor_pos;
  497. // get the highlighted words
  498. String highlighted_text = get_selection_text();
  499. for (int i=0;i<visible_rows;i++) {
  500. int line=i+cursor.line_ofs;
  501. if (line<0 || line>=(int)text.size())
  502. continue;
  503. const String &str=text[line];
  504. int char_margin=xmargin_beg-cursor.x_ofs;
  505. int char_ofs=0;
  506. int ofs_y=i*get_row_height()+cache.line_spacing/2;
  507. bool prev_is_char=false;
  508. bool prev_is_number = false;
  509. bool in_keyword=false;
  510. bool underlined=false;
  511. bool in_word = false;
  512. bool in_function_name = false;
  513. bool in_member_variable = false;
  514. bool is_hex_notation = false;
  515. Color keyword_color;
  516. // check if line contains highlighted word
  517. int highlighted_text_col = -1;
  518. int search_text_col = -1;
  519. if (!search_text.empty())
  520. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
  521. if (highlighted_text.length() != 0 && highlighted_text != search_text)
  522. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE|SEARCH_WHOLE_WORDS, 0);
  523. const Map<int,Text::ColorRegionInfo>& cri_map=text.get_color_region_info(line);
  524. if (text.is_marked(line)) {
  525. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.mark_color);
  526. }
  527. if (text.is_breakpoint(line)) {
  528. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.breakpoint_color);
  529. }
  530. if (line==cursor.line) {
  531. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(0, ofs_y,xmargin_end,get_row_height()),cache.current_line_color);
  532. }
  533. // draw breakpoint marker
  534. if (text.is_breakpoint(line)) {
  535. if (draw_breakpoint_gutter) {
  536. int vertical_gap = (get_row_height() * 40) / 100;
  537. int horizontal_gap = (cache.breakpoint_gutter_width * 30) / 100;
  538. int marker_height = get_row_height() - (vertical_gap * 2);
  539. int marker_width = cache.breakpoint_gutter_width - (horizontal_gap * 2);
  540. // no transparency on marker
  541. 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));
  542. }
  543. }
  544. if (cache.line_number_w) {
  545. String fc = String::num(line+1);
  546. while (fc.length() < line_number_char_count) {
  547. fc="0"+fc;
  548. }
  549. cache.font->draw(ci,Point2(cache.style_normal->get_margin(MARGIN_LEFT)+cache.breakpoint_gutter_width,ofs_y+cache.font->get_ascent()),fc,cache.line_number_color);
  550. }
  551. for (int j=0;j<str.length();j++) {
  552. //look for keyword
  553. if (deregion>0) {
  554. deregion--;
  555. if (deregion==0)
  556. in_region=-1;
  557. }
  558. if (syntax_coloring && deregion==0) {
  559. color = cache.font_color; //reset
  560. //find keyword
  561. bool is_char = _is_text_char(str[j]);
  562. bool is_symbol = _is_symbol(str[j]);
  563. bool is_number = _is_number(str[j]);
  564. if (j==0 && in_region>=0 && color_regions[in_region].line_only) {
  565. in_region=-1; //reset regions that end at end of line
  566. }
  567. // allow ABCDEF in hex notation
  568. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  569. is_number = true;
  570. } else {
  571. is_hex_notation = false;
  572. }
  573. // check for dot or 'x' for hex notation in floating point number
  574. if ((str[j] == '.' || str[j] == 'x') && !in_word && prev_is_number && !is_number) {
  575. is_number = true;
  576. is_symbol = false;
  577. if (str[j] == 'x' && str[j-1] == '0') {
  578. is_hex_notation = true;
  579. }
  580. }
  581. if (!in_word && _is_char(str[j])) {
  582. in_word = true;
  583. }
  584. if ((in_keyword || in_word) && !is_hex_notation) {
  585. is_number = false;
  586. }
  587. if (is_symbol && str[j] != '.' && in_word) {
  588. in_word = false;
  589. }
  590. if (is_symbol && cri_map.has(j)) {
  591. const Text::ColorRegionInfo &cri=cri_map[j];
  592. if (in_region==-1) {
  593. if (!cri.end) {
  594. in_region=cri.region;
  595. }
  596. } else if (in_region==cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
  597. if (cri.end || color_regions[cri.region].eq) {
  598. deregion=color_regions[cri.region].eq?color_regions[cri.region].begin_key.length():color_regions[cri.region].end_key.length();
  599. }
  600. }
  601. }
  602. if (!is_char) {
  603. in_keyword=false;
  604. underlined=false;
  605. }
  606. if (in_region==-1 && !in_keyword && is_char && !prev_is_char) {
  607. int to=j;
  608. while(to<str.length() && _is_text_char(str[to]))
  609. to++;
  610. uint32_t hash = String::hash(&str[j],to-j);
  611. StrRange range(&str[j],to-j);
  612. const Color *col=keywords.custom_getptr(range,hash);
  613. if (col) {
  614. in_keyword=true;
  615. keyword_color=*col;
  616. }
  617. if (select_identifiers_enabled && hilighted_word!=String()) {
  618. if (hilighted_word==range) {
  619. underlined=true;
  620. }
  621. }
  622. }
  623. if (!in_function_name && in_word && !in_keyword) {
  624. int k = j;
  625. while(k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  626. k++;
  627. }
  628. // check for space between name and bracket
  629. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  630. k++;
  631. }
  632. if (str[k] == '(') {
  633. in_function_name = true;
  634. }
  635. }
  636. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  637. int k = j;
  638. while(k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  639. k--;
  640. }
  641. if (str[k] == '.') {
  642. in_member_variable = true;
  643. }
  644. }
  645. if (is_symbol) {
  646. in_function_name = false;
  647. in_member_variable = false;
  648. }
  649. if (in_region>=0)
  650. color=color_regions[in_region].color;
  651. else if (in_keyword)
  652. color=keyword_color;
  653. else if (in_member_variable)
  654. color=cache.member_variable_color;
  655. else if (in_function_name)
  656. color=cache.function_color;
  657. else if (is_symbol)
  658. color=symbol_color;
  659. else if (is_number)
  660. color=cache.number_color;
  661. prev_is_char=is_char;
  662. prev_is_number=is_number;
  663. }
  664. int char_w;
  665. //handle tabulator
  666. if (str[j]=='\t') {
  667. int left = char_ofs%tab_w;
  668. if (left==0)
  669. char_w=tab_w;
  670. else
  671. char_w=tab_w-char_ofs%tab_w; // is right...
  672. } else {
  673. char_w=cache.font->get_char_size(str[j],str[j+1]).width;
  674. }
  675. if ( (char_ofs+char_margin)<xmargin_beg) {
  676. char_ofs+=char_w;
  677. continue;
  678. }
  679. if ( (char_ofs+char_margin+char_w)>=xmargin_end) {
  680. if (syntax_coloring)
  681. continue;
  682. else
  683. break;
  684. }
  685. bool in_search_result=false;
  686. if (search_text_col != -1) {
  687. // if we are at the end check for new search result on same line
  688. if (j >= search_text_col+search_text.length())
  689. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, j);
  690. in_search_result = j >= search_text_col && j < search_text_col+search_text.length();
  691. if (in_search_result) {
  692. 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);
  693. }
  694. }
  695. 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));
  696. if (in_selection) {
  697. //inside selection!
  698. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w,get_row_height())),cache.selection_color);
  699. }
  700. if (in_search_result) {
  701. 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;
  702. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w,1)),border_color);
  703. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y+get_row_height()-1 ), Size2i(char_w,1)),border_color);
  704. if (j==search_text_col)
  705. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(1,get_row_height())),border_color);
  706. if (j==search_text_col+search_text.length()-1)
  707. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin+char_w-1, ofs_y ), Size2i(1,get_row_height())),border_color);
  708. }
  709. if (highlight_all_occurrences) {
  710. if (highlighted_text_col != -1) {
  711. // if we are at the end check for new word on same line
  712. if (j > highlighted_text_col+highlighted_text.length()) {
  713. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE|SEARCH_WHOLE_WORDS, j);
  714. }
  715. bool in_highlighted_word = (j >= highlighted_text_col && j < highlighted_text_col+highlighted_text.length());
  716. /* if this is the original highlighted text we don't want to highlight it again */
  717. if (cursor.line==line && (cursor.column >= highlighted_text_col && cursor.column <= highlighted_text_col+highlighted_text.length())) {
  718. in_highlighted_word = false;
  719. }
  720. if (in_highlighted_word) {
  721. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w, get_row_height())),cache.word_highlighted_color);
  722. }
  723. }
  724. }
  725. if (brace_matching_enabled) {
  726. if ( (brace_open_match_line==line && brace_open_match_column==j) ||
  727. (cursor.column==j && cursor.line==line && (brace_open_matching||brace_open_mismatch))) {
  728. if (brace_open_mismatch)
  729. color=cache.brace_mismatch_color;
  730. cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),'_',str[j+1],in_selection?cache.font_selected_color:color);
  731. }
  732. if (
  733. (brace_close_match_line==line && brace_close_match_column==j) ||
  734. (cursor.column==j+1 && cursor.line==line && (brace_close_matching||brace_close_mismatch))) {
  735. if (brace_close_mismatch)
  736. color=cache.brace_mismatch_color;
  737. cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),'_',str[j+1],in_selection?cache.font_selected_color:color);
  738. }
  739. }
  740. if (cursor.column==j && cursor.line==line) {
  741. cursor_pos = Point2i( char_ofs+char_margin, ofs_y );
  742. if (insert_mode) {
  743. cursor_pos.y += (get_row_height() - 3);
  744. }
  745. int caret_w = (str[j]=='\t') ? cache.font->get_char_size(' ').width : char_w;
  746. if (draw_caret) {
  747. if (insert_mode) {
  748. int caret_h = (block_caret) ? 4 : 1;
  749. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(caret_w,caret_h)),cache.caret_color);
  750. } else {
  751. caret_w = (block_caret) ? caret_w : 1;
  752. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(caret_w,get_row_height())),cache.caret_color);
  753. }
  754. }
  755. }
  756. if (cursor.column==j && cursor.line==line && block_caret && draw_caret && !insert_mode) {
  757. color = cache.caret_background_color;
  758. }
  759. if (str[j]>=32) {
  760. int w = cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),str[j],str[j+1],in_selection?cache.font_selected_color:color);
  761. if (underlined) {
  762. draw_rect(Rect2( char_ofs+char_margin, ofs_y+ascent+2,w,1),in_selection?cache.font_selected_color:color);
  763. }
  764. }
  765. else if (draw_tabs && str[j]=='\t') {
  766. int yofs= (get_row_height() - cache.tab_icon->get_height())/2;
  767. cache.tab_icon->draw(ci, Point2(char_ofs+char_margin,ofs_y+yofs),in_selection?cache.font_selected_color:color);
  768. }
  769. char_ofs+=char_w;
  770. }
  771. if (cursor.column==str.length() && cursor.line==line && (char_ofs+char_margin)>=xmargin_beg) {
  772. cursor_pos=Point2i( char_ofs+char_margin, ofs_y );
  773. if (insert_mode) {
  774. cursor_pos.y += (get_row_height() - 3);
  775. }
  776. if (draw_caret) {
  777. if (insert_mode) {
  778. int char_w = cache.font->get_char_size(' ').width;
  779. int caret_h = (block_caret) ? 4 : 1;
  780. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,caret_h)),cache.caret_color);
  781. } else {
  782. int char_w = cache.font->get_char_size(' ').width;
  783. int caret_w = (block_caret) ? char_w : 1;
  784. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(caret_w,get_row_height())),cache.caret_color);
  785. }
  786. }
  787. }
  788. }
  789. bool completion_below = false;
  790. if (completion_active) {
  791. // code completion box
  792. Ref<StyleBox> csb = get_stylebox("completion");
  793. int maxlines = get_constant("completion_lines");
  794. int cmax_width = get_constant("completion_max_width")*cache.font->get_char_size('x').x;
  795. int scrollw = get_constant("completion_scroll_width");
  796. Color scrollc = get_color("completion_scroll_color");
  797. int lines = MIN(completion_options.size(),maxlines);
  798. int w=0;
  799. int h=lines*get_row_height();
  800. int nofs = cache.font->get_string_size(completion_base).width;
  801. if (completion_options.size() < 50) {
  802. for(int i=0;i<completion_options.size();i++) {
  803. int w2=MIN(cache.font->get_string_size(completion_options[i]).x,cmax_width);
  804. if (w2>w)
  805. w=w2;
  806. }
  807. } else {
  808. w=cmax_width;
  809. }
  810. int th = h + csb->get_minimum_size().y;
  811. if (cursor_pos.y+get_row_height()+th > get_size().height) {
  812. completion_rect.pos.y=cursor_pos.y-th;
  813. } else {
  814. completion_rect.pos.y=cursor_pos.y+get_row_height()+csb->get_offset().y;
  815. completion_below = true;
  816. }
  817. if (cursor_pos.x-nofs+w+scrollw > get_size().width) {
  818. completion_rect.pos.x=get_size().width-w-scrollw;
  819. } else {
  820. completion_rect.pos.x=cursor_pos.x-nofs;
  821. }
  822. completion_rect.size.width=w+2;
  823. completion_rect.size.height=h;
  824. if (completion_options.size()<=maxlines)
  825. scrollw=0;
  826. draw_style_box(csb,Rect2(completion_rect.pos-csb->get_offset(),completion_rect.size+csb->get_minimum_size()+Size2(scrollw,0)));
  827. if (cache.completion_background_color.a>0.01) {
  828. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(completion_rect.pos,completion_rect.size+Size2(scrollw,0)),cache.completion_background_color);
  829. }
  830. int line_from = CLAMP(completion_index - lines/2, 0, completion_options.size() - lines);
  831. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2(completion_rect.pos.x,completion_rect.pos.y+(completion_index-line_from)*get_row_height()),Size2(completion_rect.size.width,get_row_height())),cache.completion_selected_color);
  832. draw_rect(Rect2(completion_rect.pos,Size2(nofs,completion_rect.size.height)),cache.completion_existing_color);
  833. for(int i=0;i<lines;i++) {
  834. int l = line_from + i;
  835. ERR_CONTINUE( l < 0 || l>= completion_options.size());
  836. Color text_color = cache.completion_font_color;
  837. for(int j=0;j<color_regions.size();j++) {
  838. if (completion_options[l].begins_with(color_regions[j].begin_key)) {
  839. text_color=color_regions[j].color;
  840. }
  841. }
  842. draw_string(cache.font,Point2(completion_rect.pos.x,completion_rect.pos.y+i*get_row_height()+cache.font->get_ascent()),completion_options[l],text_color,completion_rect.size.width);
  843. }
  844. if (scrollw) {
  845. //draw a small scroll rectangle to show a position in the options
  846. float r = maxlines / (float)completion_options.size();
  847. float o = line_from / (float)completion_options.size();
  848. draw_rect(Rect2(completion_rect.pos.x+completion_rect.size.width,completion_rect.pos.y+o*completion_rect.size.y,scrollw,completion_rect.size.y*r),scrollc);
  849. }
  850. completion_line_ofs=line_from;
  851. }
  852. // check to see if the hint should be drawn
  853. bool show_hint = false;
  854. if (completion_hint!="") {
  855. if (completion_active) {
  856. if (completion_below && !callhint_below) {
  857. show_hint = true;
  858. }
  859. else if (!completion_below && callhint_below) {
  860. show_hint = true;
  861. }
  862. }
  863. else {
  864. show_hint = true;
  865. }
  866. }
  867. if (show_hint) {
  868. Ref<StyleBox> sb = get_stylebox("panel","TooltipPanel");
  869. Ref<Font> font = cache.font;
  870. Color font_color = get_color("font_color","TooltipLabel");
  871. int max_w=0;
  872. int sc = completion_hint.get_slice_count("\n");
  873. int offset=0;
  874. int spacing=0;
  875. for(int i=0;i<sc;i++) {
  876. String l = completion_hint.get_slice("\n",i);
  877. int len = font->get_string_size(l).x;
  878. max_w = MAX(len,max_w);
  879. if (i==0) {
  880. offset = font->get_string_size(l.substr(0,l.find(String::chr(0xFFFF)))).x;
  881. } else {
  882. spacing+=cache.line_spacing;
  883. }
  884. }
  885. Size2 size = Size2(max_w,sc*font->get_height()+spacing);
  886. Size2 minsize = size+sb->get_minimum_size();
  887. if (completion_hint_offset==-0xFFFF) {
  888. completion_hint_offset=cursor_pos.x-offset;
  889. }
  890. Point2 hint_ofs = Vector2(completion_hint_offset,cursor_pos.y) + callhint_offset;
  891. if (callhint_below) {
  892. hint_ofs.y += get_row_height() + sb->get_offset().y;
  893. }
  894. else {
  895. hint_ofs.y -= minsize.y + sb->get_offset().y;
  896. }
  897. draw_style_box(sb,Rect2(hint_ofs,minsize));
  898. spacing=0;
  899. for(int i=0;i<sc;i++) {
  900. int begin=0;
  901. int end=0;
  902. String l = completion_hint.get_slice("\n",i);
  903. if (l.find(String::chr(0xFFFF))!=-1) {
  904. begin = font->get_string_size(l.substr(0,l.find(String::chr(0xFFFF)))).x;
  905. end = font->get_string_size(l.substr(0,l.rfind(String::chr(0xFFFF)))).x;
  906. }
  907. 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);
  908. if (end>0) {
  909. Vector2 b = hint_ofs+sb->get_offset()+Vector2(begin,font->get_height()+font->get_height()*i+spacing-1);
  910. draw_line(b,b+Vector2(end-begin,0),font_color);
  911. }
  912. spacing+=cache.line_spacing;
  913. }
  914. }
  915. } break;
  916. case NOTIFICATION_FOCUS_ENTER: {
  917. if (!caret_blink_enabled) {
  918. draw_caret = true;
  919. }
  920. if (OS::get_singleton()->has_virtual_keyboard())
  921. OS::get_singleton()->show_virtual_keyboard(get_text(),get_global_rect());
  922. if (raised_from_completion) {
  923. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
  924. }
  925. } break;
  926. case NOTIFICATION_FOCUS_EXIT: {
  927. if (OS::get_singleton()->has_virtual_keyboard())
  928. OS::get_singleton()->hide_virtual_keyboard();
  929. if (raised_from_completion) {
  930. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
  931. }
  932. } break;
  933. }
  934. }
  935. void TextEdit::_consume_pair_symbol(CharType ch) {
  936. int cursor_position_to_move = cursor_get_column() + 1;
  937. CharType ch_single[2] = {ch, 0};
  938. CharType ch_single_pair[2] = {_get_right_pair_symbol(ch), 0};
  939. CharType ch_pair[3] = {ch, _get_right_pair_symbol(ch), 0};
  940. if(is_selection_active()) {
  941. int new_column,new_line;
  942. begin_complex_operation();
  943. _insert_text(get_selection_from_line(), get_selection_from_column(),
  944. ch_single,
  945. &new_line, &new_column);
  946. int to_col_offset = 0;
  947. if(get_selection_from_line() == get_selection_to_line())
  948. to_col_offset = 1;
  949. _insert_text(get_selection_to_line(),
  950. get_selection_to_column() + to_col_offset,
  951. ch_single_pair,
  952. &new_line,&new_column);
  953. end_complex_operation();
  954. cursor_set_line(get_selection_to_line());
  955. cursor_set_column(get_selection_to_column() + to_col_offset);
  956. deselect();
  957. update();
  958. return;
  959. }
  960. if( (ch == '\'' || ch == '"') &&
  961. cursor_get_column() > 0 &&
  962. _is_text_char(text[cursor.line][cursor_get_column() - 1])
  963. ) {
  964. insert_text_at_cursor(ch_single);
  965. cursor_set_column(cursor_position_to_move);
  966. return;
  967. }
  968. if(cursor_get_column() < text[cursor.line].length()) {
  969. if(_is_text_char(text[cursor.line][cursor_get_column()])) {
  970. insert_text_at_cursor(ch_single);
  971. cursor_set_column(cursor_position_to_move);
  972. return;
  973. }
  974. if( _is_pair_right_symbol(ch) &&
  975. text[cursor.line][cursor_get_column()] == ch
  976. ) {
  977. cursor_set_column(cursor_position_to_move);
  978. return;
  979. }
  980. }
  981. insert_text_at_cursor(ch_pair);
  982. cursor_set_column(cursor_position_to_move);
  983. return;
  984. }
  985. void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
  986. bool remove_right_symbol = false;
  987. if(cursor.column < text[cursor.line].length() && cursor.column > 0) {
  988. CharType left_char = text[cursor.line][cursor.column - 1];
  989. CharType right_char = text[cursor.line][cursor.column];
  990. if(right_char == _get_right_pair_symbol(left_char)) {
  991. remove_right_symbol = true;
  992. }
  993. }
  994. if(remove_right_symbol) {
  995. _remove_text(prev_line,prev_column,cursor.line,cursor.column + 1);
  996. } else {
  997. _remove_text(prev_line,prev_column,cursor.line,cursor.column);
  998. }
  999. }
  1000. void TextEdit::backspace_at_cursor() {
  1001. if (readonly)
  1002. return;
  1003. if (cursor.column==0 && cursor.line==0)
  1004. return;
  1005. int prev_line = cursor.column?cursor.line:cursor.line-1;
  1006. int prev_column = cursor.column?(cursor.column-1):(text[cursor.line-1].length());
  1007. if(auto_brace_completion_enabled &&
  1008. cursor.column > 0 &&
  1009. _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
  1010. _consume_backspace_for_pair_symbol(prev_line, prev_column);
  1011. } else {
  1012. _remove_text(prev_line,prev_column,cursor.line,cursor.column);
  1013. }
  1014. cursor_set_line(prev_line);
  1015. cursor_set_column(prev_column);
  1016. }
  1017. void TextEdit::indent_selection_right() {
  1018. if (!is_selection_active()) {
  1019. return;
  1020. }
  1021. begin_complex_operation();
  1022. int start_line = get_selection_from_line();
  1023. int end_line = get_selection_to_line();
  1024. // ignore if the cursor is not past the first column
  1025. if (get_selection_to_column() == 0) {
  1026. end_line--;
  1027. }
  1028. for (int i = start_line; i <= end_line; i++) {
  1029. String line_text = get_line(i);
  1030. line_text = '\t' + line_text;
  1031. set_line(i, line_text);
  1032. }
  1033. // fix selection being off by one on the last line
  1034. selection.to_column++;
  1035. end_complex_operation();
  1036. update();
  1037. }
  1038. void TextEdit::indent_selection_left() {
  1039. if (!is_selection_active()) {
  1040. return;
  1041. }
  1042. begin_complex_operation();
  1043. int start_line = get_selection_from_line();
  1044. int end_line = get_selection_to_line();
  1045. // ignore if the cursor is not past the first column
  1046. if (get_selection_to_column() == 0) {
  1047. end_line--;
  1048. }
  1049. String last_line_text = get_line(end_line);
  1050. for (int i = start_line; i <= end_line; i++) {
  1051. String line_text = get_line(i);
  1052. if (line_text.begins_with("\t")) {
  1053. line_text = line_text.substr(1, line_text.length());
  1054. set_line(i, line_text);
  1055. } else if (line_text.begins_with(" ")) {
  1056. line_text = line_text.substr(4, line_text.length());
  1057. set_line(i, line_text);
  1058. }
  1059. }
  1060. // fix selection being off by one on the last line
  1061. if (last_line_text != get_line(end_line) && selection.to_column > 0) {
  1062. selection.to_column--;
  1063. }
  1064. end_complex_operation();
  1065. update();
  1066. }
  1067. void TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
  1068. float rows=p_mouse.y;
  1069. rows-=cache.style_normal->get_margin(MARGIN_TOP);
  1070. rows/=get_row_height();
  1071. int row=cursor.line_ofs+rows;
  1072. if (row<0)
  1073. row=0;
  1074. int col=0;
  1075. if (row>=text.size()) {
  1076. row=text.size()-1;
  1077. col=text[row].size();
  1078. } else {
  1079. col=p_mouse.x-(cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width);
  1080. col+=cursor.x_ofs;
  1081. col=get_char_pos_for( col, get_line(row) );
  1082. }
  1083. r_row=row;
  1084. r_col=col;
  1085. }
  1086. void TextEdit::_input_event(const InputEvent& p_input_event) {
  1087. switch(p_input_event.type) {
  1088. case InputEvent::MOUSE_BUTTON: {
  1089. const InputEventMouseButton &mb=p_input_event.mouse_button;
  1090. if (completion_active && completion_rect.has_point(Point2(mb.x,mb.y))) {
  1091. if (!mb.pressed)
  1092. return;
  1093. if (mb.button_index==BUTTON_WHEEL_UP) {
  1094. if (completion_index>0) {
  1095. completion_index--;
  1096. completion_current=completion_options[completion_index];
  1097. update();
  1098. }
  1099. }
  1100. if (mb.button_index==BUTTON_WHEEL_DOWN) {
  1101. if (completion_index<completion_options.size()-1) {
  1102. completion_index++;
  1103. completion_current=completion_options[completion_index];
  1104. update();
  1105. }
  1106. }
  1107. if (mb.button_index==BUTTON_LEFT) {
  1108. completion_index=CLAMP(completion_line_ofs+(mb.y-completion_rect.pos.y)/get_row_height(),0,completion_options.size()-1);
  1109. completion_current=completion_options[completion_index];
  1110. update();
  1111. if (mb.doubleclick)
  1112. _confirm_completion();
  1113. }
  1114. return;
  1115. } else {
  1116. _cancel_completion();
  1117. _cancel_code_hint();
  1118. }
  1119. if (mb.pressed) {
  1120. if (mb.button_index==BUTTON_WHEEL_UP && !mb.mod.command) {
  1121. v_scroll->set_val( v_scroll->get_val() -3 );
  1122. }
  1123. if (mb.button_index==BUTTON_WHEEL_DOWN && !mb.mod.command) {
  1124. v_scroll->set_val( v_scroll->get_val() +3 );
  1125. }
  1126. if (mb.button_index==BUTTON_WHEEL_LEFT) {
  1127. h_scroll->set_val( h_scroll->get_val() -3 );
  1128. }
  1129. if (mb.button_index==BUTTON_WHEEL_RIGHT) {
  1130. h_scroll->set_val( h_scroll->get_val() +3 );
  1131. }
  1132. if (mb.button_index==BUTTON_LEFT) {
  1133. _reset_caret_blink_timer();
  1134. int row,col;
  1135. _get_mouse_pos(Point2i(mb.x,mb.y), row,col);
  1136. if (mb.mod.command && hilighted_word!=String()) {
  1137. emit_signal("symbol_lookup",hilighted_word,row,col);
  1138. return;
  1139. }
  1140. // toggle breakpoint on gutter click
  1141. if (draw_breakpoint_gutter) {
  1142. int gutter=cache.style_normal->get_margin(MARGIN_LEFT);
  1143. if (mb.x > gutter && mb.x <= gutter + cache.breakpoint_gutter_width + 3) {
  1144. set_line_as_breakpoint(row, !is_line_set_as_breakpoint(row));
  1145. emit_signal("breakpoint_toggled", row);
  1146. return;
  1147. }
  1148. }
  1149. int prev_col=cursor.column;
  1150. int prev_line=cursor.line;
  1151. cursor_set_line( row );
  1152. cursor_set_column( col );
  1153. if (mb.mod.shift && (cursor.column!=prev_col || cursor.line!=prev_line)) {
  1154. if (!selection.active) {
  1155. selection.active=true;
  1156. selection.selecting_mode=Selection::MODE_POINTER;
  1157. selection.from_column=prev_col;
  1158. selection.from_line=prev_line;
  1159. selection.to_column=cursor.column;
  1160. selection.to_line=cursor.line;
  1161. if (selection.from_line>selection.to_line || (selection.from_line==selection.to_line && selection.from_column>selection.to_column)) {
  1162. SWAP(selection.from_column,selection.to_column);
  1163. SWAP(selection.from_line,selection.to_line);
  1164. selection.shiftclick_left=false;
  1165. } else {
  1166. selection.shiftclick_left=true;
  1167. }
  1168. selection.selecting_line=prev_line;
  1169. selection.selecting_column=prev_col;
  1170. update();
  1171. } else {
  1172. if (cursor.line<selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column<selection.selecting_column)) {
  1173. if (selection.shiftclick_left) {
  1174. SWAP(selection.from_column,selection.to_column);
  1175. SWAP(selection.from_line,selection.to_line);
  1176. selection.shiftclick_left = !selection.shiftclick_left;
  1177. }
  1178. selection.from_column=cursor.column;
  1179. selection.from_line=cursor.line;
  1180. } else if (cursor.line>selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column>selection.selecting_column)) {
  1181. if (!selection.shiftclick_left) {
  1182. SWAP(selection.from_column,selection.to_column);
  1183. SWAP(selection.from_line,selection.to_line);
  1184. selection.shiftclick_left = !selection.shiftclick_left;
  1185. }
  1186. selection.to_column=cursor.column;
  1187. selection.to_line=cursor.line;
  1188. } else {
  1189. selection.active=false;
  1190. }
  1191. update();
  1192. }
  1193. } else {
  1194. //if sel active and dblick last time < something
  1195. //else
  1196. selection.active=false;
  1197. selection.selecting_mode=Selection::MODE_POINTER;
  1198. selection.selecting_line=row;
  1199. selection.selecting_column=col;
  1200. }
  1201. if (!mb.doubleclick && (OS::get_singleton()->get_ticks_msec()-last_dblclk)<600 && cursor.line==prev_line) {
  1202. //tripleclick select line
  1203. select(cursor.line,0,cursor.line,text[cursor.line].length());
  1204. selection.selecting_column=0;
  1205. last_dblclk=0;
  1206. } else if (mb.doubleclick && text[cursor.line].length()) {
  1207. //doubleclick select world
  1208. String s = text[cursor.line];
  1209. int beg=CLAMP(cursor.column,0,s.length());
  1210. int end=beg;
  1211. if (s[beg]>32 || beg==s.length()) {
  1212. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  1213. while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) {
  1214. beg--;
  1215. }
  1216. while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) {
  1217. end++;
  1218. }
  1219. if (end<s.length())
  1220. end+=1;
  1221. select(cursor.line,beg,cursor.line,end);
  1222. selection.selecting_column=beg;
  1223. }
  1224. last_dblclk = OS::get_singleton()->get_ticks_msec();
  1225. }
  1226. update();
  1227. }
  1228. if (mb.button_index==BUTTON_RIGHT && context_menu_enabled) {
  1229. menu->set_pos(get_global_transform().xform(get_local_mouse_pos()));
  1230. menu->set_size(Vector2(1,1));
  1231. menu->popup();
  1232. grab_focus();
  1233. }
  1234. } else {
  1235. if (mb.button_index==BUTTON_LEFT)
  1236. click_select_held->stop();
  1237. // notify to show soft keyboard
  1238. notification(NOTIFICATION_FOCUS_ENTER);
  1239. }
  1240. } break;
  1241. case InputEvent::MOUSE_MOTION: {
  1242. const InputEventMouseMotion &mm=p_input_event.mouse_motion;
  1243. if (select_identifiers_enabled) {
  1244. if (mm.mod.command && mm.button_mask==0) {
  1245. String new_word = get_word_at_pos(Vector2(mm.x,mm.y));
  1246. if (new_word!=hilighted_word) {
  1247. hilighted_word=new_word;
  1248. update();
  1249. }
  1250. } else {
  1251. if (hilighted_word!=String()) {
  1252. hilighted_word=String();
  1253. update();
  1254. }
  1255. }
  1256. }
  1257. if (mm.button_mask&BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data()==Variant()) { //ignore if dragging
  1258. if (selection.selecting_mode!=Selection::MODE_NONE) {
  1259. _reset_caret_blink_timer();
  1260. int row,col;
  1261. _get_mouse_pos(Point2i(mm.x,mm.y), row,col);
  1262. select(selection.selecting_line,selection.selecting_column,row,col);
  1263. cursor_set_line( row );
  1264. cursor_set_column( col );
  1265. update();
  1266. click_select_held->start();
  1267. }
  1268. }
  1269. } break;
  1270. case InputEvent::KEY: {
  1271. InputEventKey k=p_input_event.key;
  1272. #ifdef OSX_ENABLED
  1273. if (k.scancode==KEY_META) {
  1274. #else
  1275. if (k.scancode==KEY_CONTROL) {
  1276. #endif
  1277. if (select_identifiers_enabled) {
  1278. if (k.pressed) {
  1279. hilighted_word = get_word_at_pos(get_local_mouse_pos());
  1280. update();
  1281. } else {
  1282. hilighted_word=String();
  1283. update();
  1284. }
  1285. }
  1286. }
  1287. if (!k.pressed)
  1288. return;
  1289. if (completion_active) {
  1290. if (readonly)
  1291. break;
  1292. bool valid=true;
  1293. if (k.mod.command || k.mod.meta)
  1294. valid=false;
  1295. if (valid) {
  1296. if (!k.mod.alt) {
  1297. if (k.scancode==KEY_UP) {
  1298. if (completion_index>0) {
  1299. completion_index--;
  1300. completion_current=completion_options[completion_index];
  1301. update();
  1302. }
  1303. accept_event();
  1304. return;
  1305. }
  1306. if (k.scancode==KEY_DOWN) {
  1307. if (completion_index<completion_options.size()-1) {
  1308. completion_index++;
  1309. completion_current=completion_options[completion_index];
  1310. update();
  1311. }
  1312. accept_event();
  1313. return;
  1314. }
  1315. if (k.scancode==KEY_PAGEUP) {
  1316. completion_index-=get_constant("completion_lines");
  1317. if (completion_index<0)
  1318. completion_index=0;
  1319. completion_current=completion_options[completion_index];
  1320. update();
  1321. accept_event();
  1322. return;
  1323. }
  1324. if (k.scancode==KEY_PAGEDOWN) {
  1325. completion_index+=get_constant("completion_lines");
  1326. if (completion_index>=completion_options.size())
  1327. completion_index=completion_options.size()-1;
  1328. completion_current=completion_options[completion_index];
  1329. update();
  1330. accept_event();
  1331. return;
  1332. }
  1333. if (k.scancode==KEY_HOME && completion_index>0) {
  1334. completion_index=0;
  1335. completion_current=completion_options[completion_index];
  1336. update();
  1337. accept_event();
  1338. return;
  1339. }
  1340. if (k.scancode==KEY_END && completion_index<completion_options.size()-1) {
  1341. completion_index=completion_options.size()-1;
  1342. completion_current=completion_options[completion_index];
  1343. update();
  1344. accept_event();
  1345. return;
  1346. }
  1347. if (k.scancode==KEY_DOWN) {
  1348. if (completion_index<completion_options.size()-1) {
  1349. completion_index++;
  1350. completion_current=completion_options[completion_index];
  1351. update();
  1352. }
  1353. accept_event();
  1354. return;
  1355. }
  1356. if (k.scancode==KEY_ENTER || k.scancode==KEY_RETURN || k.scancode==KEY_TAB) {
  1357. _confirm_completion();
  1358. accept_event();
  1359. return;
  1360. }
  1361. if (k.scancode==KEY_BACKSPACE) {
  1362. _reset_caret_blink_timer();
  1363. backspace_at_cursor();
  1364. _update_completion_candidates();
  1365. accept_event();
  1366. return;
  1367. }
  1368. if (k.scancode==KEY_SHIFT) {
  1369. accept_event();
  1370. return;
  1371. }
  1372. }
  1373. if (k.unicode>32) {
  1374. _reset_caret_blink_timer();
  1375. const CharType chr[2] = {(CharType)k.unicode, 0};
  1376. if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  1377. _consume_pair_symbol(chr[0]);
  1378. } else {
  1379. // remove the old character if in insert mode
  1380. if (insert_mode) {
  1381. begin_complex_operation();
  1382. // make sure we don't try and remove empty space
  1383. if (cursor.column < get_line(cursor.line).length()) {
  1384. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  1385. }
  1386. }
  1387. _insert_text_at_cursor(chr);
  1388. if (insert_mode) {
  1389. end_complex_operation();
  1390. }
  1391. }
  1392. _update_completion_candidates();
  1393. accept_event();
  1394. return;
  1395. }
  1396. }
  1397. _cancel_completion();
  1398. }
  1399. /* TEST CONTROL FIRST!! */
  1400. // some remaps for duplicate functions..
  1401. if (k.mod.command && !k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode==KEY_INSERT) {
  1402. k.scancode=KEY_C;
  1403. }
  1404. if (!k.mod.command && k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode==KEY_INSERT) {
  1405. k.scancode=KEY_V;
  1406. k.mod.command=true;
  1407. k.mod.shift=false;
  1408. }
  1409. if (!k.mod.command) {
  1410. _reset_caret_blink_timer();
  1411. }
  1412. // save here for insert mode, just in case it is cleared in the following section
  1413. bool had_selection = selection.active;
  1414. // stuff to do when selection is active..
  1415. if (selection.active) {
  1416. if (readonly)
  1417. break;
  1418. bool clear=false;
  1419. bool unselect=false;
  1420. bool dobreak=false;
  1421. switch(k.scancode) {
  1422. case KEY_TAB: {
  1423. if (k.mod.shift) {
  1424. indent_selection_left();
  1425. } else {
  1426. indent_selection_right();
  1427. }
  1428. dobreak=true;
  1429. accept_event();
  1430. } break;
  1431. case KEY_X:
  1432. case KEY_C:
  1433. //special keys often used with control, wait...
  1434. clear=(!k.mod.command || k.mod.shift || k.mod.alt );
  1435. break;
  1436. case KEY_DELETE:
  1437. if (!k.mod.shift) {
  1438. accept_event();
  1439. clear=true; dobreak=true;
  1440. } else if (k.mod.command || k.mod.alt) {
  1441. dobreak=true;
  1442. }
  1443. break;
  1444. case KEY_BACKSPACE:
  1445. accept_event();
  1446. clear=true; dobreak=true;
  1447. break;
  1448. case KEY_LEFT:
  1449. case KEY_RIGHT:
  1450. case KEY_UP:
  1451. case KEY_DOWN:
  1452. case KEY_PAGEUP:
  1453. case KEY_PAGEDOWN:
  1454. case KEY_HOME:
  1455. case KEY_END:
  1456. // ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys)
  1457. if (k.mod.command || k.mod.shift || k.mod.alt)
  1458. break;
  1459. unselect=true;
  1460. break;
  1461. default:
  1462. if (k.unicode>=32 && !k.mod.command && !k.mod.alt && !k.mod.meta)
  1463. clear=true;
  1464. if (auto_brace_completion_enabled && _is_pair_left_symbol(k.unicode))
  1465. clear=false;
  1466. }
  1467. if (unselect) {
  1468. selection.active=false;
  1469. selection.selecting_mode=Selection::MODE_NONE;
  1470. update();
  1471. }
  1472. if (clear) {
  1473. if (!dobreak) {
  1474. begin_complex_operation();
  1475. }
  1476. selection.active=false;
  1477. update();
  1478. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1479. cursor_set_line(selection.from_line);
  1480. cursor_set_column(selection.from_column);
  1481. update();
  1482. }
  1483. if (dobreak)
  1484. break;
  1485. }
  1486. selection.selecting_text=false;
  1487. bool scancode_handled=true;
  1488. // special scancode test...
  1489. switch (k.scancode) {
  1490. case KEY_ENTER:
  1491. case KEY_RETURN: {
  1492. if (readonly)
  1493. break;
  1494. String ins="\n";
  1495. //keep indentation
  1496. for(int i=0;i<text[cursor.line].length();i++) {
  1497. if (text[cursor.line][i]=='\t')
  1498. ins+="\t";
  1499. else
  1500. break;
  1501. }
  1502. if(auto_indent){
  1503. // indent once again if previous line will end with ':'
  1504. // (i.e. colon precedes current cursor position)
  1505. if(cursor.column>0 && text[cursor.line][cursor.column-1]==':') {
  1506. ins+="\t";
  1507. }
  1508. }
  1509. bool first_line = false;
  1510. if (k.mod.command) {
  1511. if (k.mod.shift) {
  1512. if (cursor.line > 0) {
  1513. cursor_set_line(cursor.line - 1);
  1514. cursor_set_column(text[cursor.line].length());
  1515. }
  1516. else {
  1517. cursor_set_column(0);
  1518. first_line = true;
  1519. }
  1520. }
  1521. else {
  1522. cursor_set_column(text[cursor.line].length());
  1523. }
  1524. }
  1525. _insert_text_at_cursor(ins);
  1526. _push_current_op();
  1527. if (first_line) {
  1528. cursor_set_line(0);
  1529. }
  1530. } break;
  1531. case KEY_ESCAPE: {
  1532. if (completion_hint!="") {
  1533. completion_hint="";
  1534. update();
  1535. } else {
  1536. scancode_handled=false;
  1537. }
  1538. } break;
  1539. case KEY_TAB: {
  1540. if (k.mod.command) break; // avoid tab when command
  1541. if (readonly)
  1542. break;
  1543. if (selection.active) {
  1544. } else {
  1545. if (k.mod.shift) {
  1546. int cc = cursor.column;
  1547. if (cc>0 && cc<=text[cursor.line].length() && text[cursor.line][cursor.column-1]=='\t') {
  1548. //simple unindent
  1549. backspace_at_cursor();
  1550. }
  1551. } else {
  1552. //simple indent
  1553. _insert_text_at_cursor("\t");
  1554. }
  1555. }
  1556. } break;
  1557. case KEY_BACKSPACE: {
  1558. if (readonly)
  1559. break;
  1560. #ifdef APPLE_STYLE_KEYS
  1561. if (k.mod.alt) {
  1562. #else
  1563. if (k.mod.alt) {
  1564. scancode_handled=false;
  1565. break;
  1566. } else if (k.mod.command) {
  1567. #endif
  1568. int line=cursor.line;
  1569. int column=cursor.column;
  1570. bool prev_char=false;
  1571. bool only_whitespace=true;
  1572. while (only_whitespace && line > 0) {
  1573. while (column>0) {
  1574. CharType c=text[line][column-1];
  1575. if (c != '\t' && c != ' ') {
  1576. only_whitespace=false;
  1577. break;
  1578. }
  1579. column--;
  1580. }
  1581. if (only_whitespace) {
  1582. line--;
  1583. column=text[line].length();
  1584. }
  1585. }
  1586. while (column>0) {
  1587. bool ischar=_is_text_char(text[line][column-1]);
  1588. if (prev_char && !ischar)
  1589. break;
  1590. prev_char=ischar;
  1591. column--;
  1592. }
  1593. _remove_text(line, column, cursor.line, cursor.column);
  1594. cursor_set_line(line);
  1595. cursor_set_column(column);
  1596. } else {
  1597. backspace_at_cursor();
  1598. }
  1599. } break;
  1600. case KEY_KP_4: {
  1601. if (k.unicode != 0) {
  1602. scancode_handled = false;
  1603. break;
  1604. }
  1605. // numlock disabled. fallthrough to key_left
  1606. }
  1607. case KEY_LEFT: {
  1608. if (k.mod.shift)
  1609. _pre_shift_selection();
  1610. #ifdef APPLE_STYLE_KEYS
  1611. else
  1612. #else
  1613. else if (!k.mod.alt)
  1614. #endif
  1615. deselect();
  1616. #ifdef APPLE_STYLE_KEYS
  1617. if (k.mod.command) {
  1618. cursor_set_column(0);
  1619. } else if (k.mod.alt) {
  1620. #else
  1621. if (k.mod.alt) {
  1622. scancode_handled=false;
  1623. break;
  1624. } else if (k.mod.command) {
  1625. #endif
  1626. bool prev_char=false;
  1627. int cc=cursor.column;
  1628. while (cc>0) {
  1629. bool ischar=_is_text_char(text[cursor.line][cc-1]);
  1630. if (prev_char && !ischar)
  1631. break;
  1632. prev_char=ischar;
  1633. cc--;
  1634. }
  1635. cursor_set_column(cc);
  1636. } else if (cursor.column==0) {
  1637. if (cursor.line>0) {
  1638. cursor_set_line(cursor.line-1);
  1639. cursor_set_column(text[cursor.line].length());
  1640. }
  1641. } else {
  1642. cursor_set_column(cursor_get_column()-1);
  1643. }
  1644. if (k.mod.shift)
  1645. _post_shift_selection();
  1646. } break;
  1647. case KEY_KP_6: {
  1648. if (k.unicode != 0) {
  1649. scancode_handled = false;
  1650. break;
  1651. }
  1652. // numlock disabled. fallthrough to key_right
  1653. }
  1654. case KEY_RIGHT: {
  1655. if (k.mod.shift)
  1656. _pre_shift_selection();
  1657. #ifdef APPLE_STYLE_KEYS
  1658. else
  1659. #else
  1660. else if (!k.mod.alt)
  1661. #endif
  1662. deselect();
  1663. #ifdef APPLE_STYLE_KEYS
  1664. if (k.mod.command) {
  1665. cursor_set_column(text[cursor.line].length());
  1666. } else if (k.mod.alt) {
  1667. #else
  1668. if (k.mod.alt) {
  1669. scancode_handled=false;
  1670. break;
  1671. } else if (k.mod.command) {
  1672. #endif
  1673. bool prev_char=false;
  1674. int cc=cursor.column;
  1675. while (cc<text[cursor.line].length()) {
  1676. bool ischar=_is_text_char(text[cursor.line][cc]);
  1677. if (prev_char && !ischar)
  1678. break;
  1679. prev_char=ischar;
  1680. cc++;
  1681. }
  1682. cursor_set_column(cc);
  1683. } else if (cursor.column==text[cursor.line].length()) {
  1684. if (cursor.line<text.size()-1) {
  1685. cursor_set_line(cursor.line+1);
  1686. cursor_set_column(0);
  1687. }
  1688. } else {
  1689. cursor_set_column(cursor_get_column()+1);
  1690. }
  1691. if (k.mod.shift)
  1692. _post_shift_selection();
  1693. } break;
  1694. case KEY_KP_8: {
  1695. if (k.unicode != 0) {
  1696. scancode_handled = false;
  1697. break;
  1698. }
  1699. // numlock disabled. fallthrough to key_up
  1700. }
  1701. case KEY_UP: {
  1702. if (k.mod.shift)
  1703. _pre_shift_selection();
  1704. if (k.mod.alt) {
  1705. scancode_handled=false;
  1706. break;
  1707. }
  1708. #ifndef APPLE_STYLE_KEYS
  1709. if (k.mod.command) {
  1710. _scroll_lines_up();
  1711. break;
  1712. }
  1713. #else
  1714. if (k.mod.command && k.mod.alt) {
  1715. _scroll_lines_up();
  1716. break;
  1717. }
  1718. if (k.mod.command)
  1719. cursor_set_line(0);
  1720. else
  1721. #endif
  1722. cursor_set_line(cursor_get_line()-1);
  1723. if (k.mod.shift)
  1724. _post_shift_selection();
  1725. _cancel_code_hint();
  1726. } break;
  1727. case KEY_KP_2: {
  1728. if (k.unicode != 0) {
  1729. scancode_handled = false;
  1730. break;
  1731. }
  1732. // numlock disabled. fallthrough to key_down
  1733. }
  1734. case KEY_DOWN: {
  1735. if (k.mod.shift)
  1736. _pre_shift_selection();
  1737. if (k.mod.alt) {
  1738. scancode_handled=false;
  1739. break;
  1740. }
  1741. #ifndef APPLE_STYLE_KEYS
  1742. if (k.mod.command) {
  1743. _scroll_lines_down();
  1744. break;
  1745. }
  1746. #else
  1747. if (k.mod.command && k.mod.alt) {
  1748. _scroll_lines_down();
  1749. break;
  1750. }
  1751. if (k.mod.command)
  1752. cursor_set_line(text.size()-1);
  1753. else
  1754. #endif
  1755. cursor_set_line(cursor_get_line()+1);
  1756. if (k.mod.shift)
  1757. _post_shift_selection();
  1758. _cancel_code_hint();
  1759. } break;
  1760. case KEY_DELETE: {
  1761. if (readonly)
  1762. break;
  1763. if (k.mod.shift && !k.mod.command && !k.mod.alt) {
  1764. cut();
  1765. break;
  1766. }
  1767. int curline_len = text[cursor.line].length();
  1768. if (cursor.line==text.size()-1 && cursor.column==curline_len)
  1769. break; //nothing to do
  1770. int next_line=cursor.column<curline_len?cursor.line:cursor.line+1;
  1771. int next_column;
  1772. #ifdef APPLE_STYLE_KEYS
  1773. if (k.mod.alt) {
  1774. #else
  1775. if (k.mod.alt) {
  1776. scancode_handled=false;
  1777. break;
  1778. } else if (k.mod.command) {
  1779. #endif
  1780. int last_line=text.size()-1;
  1781. int line=cursor.line;
  1782. int column=cursor.column;
  1783. bool prev_char=false;
  1784. bool only_whitespace=true;
  1785. while (only_whitespace && line < last_line) {
  1786. while (column<text[line].length()) {
  1787. CharType c=text[line][column];
  1788. if (c != '\t' && c != ' ') {
  1789. only_whitespace=false;
  1790. break;
  1791. }
  1792. column++;
  1793. }
  1794. if (only_whitespace) {
  1795. line++;
  1796. column=0;
  1797. }
  1798. }
  1799. while (column<text[line].length()) {
  1800. bool ischar=_is_text_char(text[line][column]);
  1801. if (prev_char && !ischar)
  1802. break;
  1803. prev_char=ischar;
  1804. column++;
  1805. }
  1806. next_line=line;
  1807. next_column=column;
  1808. } else {
  1809. next_column=cursor.column<curline_len?(cursor.column+1):0;
  1810. }
  1811. _remove_text(cursor.line,cursor.column,next_line,next_column);
  1812. update();
  1813. } break;
  1814. case KEY_KP_7: {
  1815. if (k.unicode != 0) {
  1816. scancode_handled = false;
  1817. break;
  1818. }
  1819. // numlock disabled. fallthrough to key_home
  1820. }
  1821. #ifdef APPLE_STYLE_KEYS
  1822. case KEY_HOME: {
  1823. if (k.mod.shift)
  1824. _pre_shift_selection();
  1825. cursor_set_line(0);
  1826. if (k.mod.shift)
  1827. _post_shift_selection();
  1828. } break;
  1829. #else
  1830. case KEY_HOME: {
  1831. if (k.mod.shift)
  1832. _pre_shift_selection();
  1833. // compute whitespace symbols seq length
  1834. int current_line_whitespace_len = 0;
  1835. while(current_line_whitespace_len < text[cursor.line].length()) {
  1836. CharType c = text[cursor.line][current_line_whitespace_len];
  1837. if(c != '\t' && c != ' ')
  1838. break;
  1839. current_line_whitespace_len++;
  1840. }
  1841. if(cursor_get_column() == current_line_whitespace_len)
  1842. cursor_set_column(0);
  1843. else
  1844. cursor_set_column(current_line_whitespace_len);
  1845. if (k.mod.command)
  1846. cursor_set_line(0);
  1847. if (k.mod.shift)
  1848. _post_shift_selection();
  1849. _cancel_completion();
  1850. completion_hint="";
  1851. } break;
  1852. #endif
  1853. case KEY_KP_1: {
  1854. if (k.unicode != 0) {
  1855. scancode_handled = false;
  1856. break;
  1857. }
  1858. // numlock disabled. fallthrough to key_end
  1859. }
  1860. #ifdef APPLE_STYLE_KEYS
  1861. case KEY_END: {
  1862. if (k.mod.shift)
  1863. _pre_shift_selection();
  1864. cursor_set_line(text.size()-1);
  1865. if (k.mod.shift)
  1866. _post_shift_selection();
  1867. } break;
  1868. #else
  1869. case KEY_END: {
  1870. if (k.mod.shift)
  1871. _pre_shift_selection();
  1872. if (k.mod.command)
  1873. cursor_set_line(text.size()-1);
  1874. cursor_set_column(text[cursor.line].length());
  1875. if (k.mod.shift)
  1876. _post_shift_selection();
  1877. _cancel_completion();
  1878. completion_hint="";
  1879. } break;
  1880. #endif
  1881. case KEY_KP_9: {
  1882. if (k.unicode != 0) {
  1883. scancode_handled = false;
  1884. break;
  1885. }
  1886. // numlock disabled. fallthrough to key_pageup
  1887. }
  1888. case KEY_PAGEUP: {
  1889. if (k.mod.shift)
  1890. _pre_shift_selection();
  1891. cursor_set_line(cursor_get_line()-get_visible_rows());
  1892. if (k.mod.shift)
  1893. _post_shift_selection();
  1894. _cancel_completion();
  1895. completion_hint="";
  1896. } break;
  1897. case KEY_KP_3: {
  1898. if (k.unicode != 0) {
  1899. scancode_handled = false;
  1900. break;
  1901. }
  1902. // numlock disabled. fallthrough to key_pageup
  1903. }
  1904. case KEY_PAGEDOWN: {
  1905. if (k.mod.shift)
  1906. _pre_shift_selection();
  1907. cursor_set_line(cursor_get_line()+get_visible_rows());
  1908. if (k.mod.shift)
  1909. _post_shift_selection();
  1910. _cancel_completion();
  1911. completion_hint="";
  1912. } break;
  1913. case KEY_A: {
  1914. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1915. scancode_handled=false;
  1916. break;
  1917. }
  1918. select_all();
  1919. } break;
  1920. case KEY_X: {
  1921. if (readonly) {
  1922. break;
  1923. }
  1924. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1925. scancode_handled=false;
  1926. break;
  1927. }
  1928. cut();
  1929. } break;
  1930. case KEY_C: {
  1931. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1932. scancode_handled=false;
  1933. break;
  1934. }
  1935. copy();
  1936. } break;
  1937. case KEY_Z: {
  1938. if (!k.mod.command) {
  1939. scancode_handled=false;
  1940. break;
  1941. }
  1942. if (k.mod.shift)
  1943. redo();
  1944. else
  1945. undo();
  1946. } break;
  1947. case KEY_V: {
  1948. if (readonly) {
  1949. break;
  1950. }
  1951. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1952. scancode_handled=false;
  1953. break;
  1954. }
  1955. paste();
  1956. } break;
  1957. case KEY_SPACE: {
  1958. #ifdef OSX_ENABLED
  1959. if (completion_enabled && k.mod.meta) { //cmd-space is spotlight shortcut in OSX
  1960. #else
  1961. if (completion_enabled && k.mod.command) {
  1962. #endif
  1963. query_code_comple();
  1964. scancode_handled=true;
  1965. } else {
  1966. scancode_handled=false;
  1967. }
  1968. } break;
  1969. case KEY_U:{
  1970. if (!k.mod.command || k.mod.shift) {
  1971. scancode_handled=false;
  1972. break;
  1973. }
  1974. else {
  1975. if (selection.active) {
  1976. int ini = selection.from_line;
  1977. int end = selection.to_line;
  1978. for (int i=ini; i<= end; i++)
  1979. {
  1980. if (text[i][0] == '#')
  1981. _remove_text(i,0,i,1);
  1982. }
  1983. }
  1984. else{
  1985. if (text[cursor.line][0] == '#')
  1986. _remove_text(cursor.line,0,cursor.line,1);
  1987. }
  1988. update();
  1989. }
  1990. } break;
  1991. default: {
  1992. scancode_handled=false;
  1993. } break;
  1994. }
  1995. if (scancode_handled)
  1996. accept_event();
  1997. /*
  1998. if (!scancode_handled && !k.mod.command && !k.mod.alt) {
  1999. if (k.unicode>=32) {
  2000. if (readonly)
  2001. break;
  2002. accept_event();
  2003. } else {
  2004. break;
  2005. }
  2006. }
  2007. */
  2008. if (k.scancode==KEY_INSERT) {
  2009. set_insert_mode(!insert_mode);
  2010. accept_event();
  2011. return;
  2012. }
  2013. if (!scancode_handled && !k.mod.command) { //for german kbds
  2014. if (k.unicode>=32) {
  2015. if (readonly)
  2016. break;
  2017. // remove the old character if in insert mode and no selection
  2018. if (insert_mode && !had_selection) {
  2019. begin_complex_operation();
  2020. // make sure we don't try and remove empty space
  2021. if (cursor.column < get_line(cursor.line).length()) {
  2022. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  2023. }
  2024. }
  2025. const CharType chr[2] = {(CharType)k.unicode, 0};
  2026. if (completion_hint!="" && k.unicode==')') {
  2027. completion_hint="";
  2028. }
  2029. if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  2030. _consume_pair_symbol(chr[0]);
  2031. } else {
  2032. _insert_text_at_cursor(chr);
  2033. }
  2034. if (insert_mode && !had_selection) {
  2035. end_complex_operation();
  2036. }
  2037. if (selection.active != had_selection) {
  2038. end_complex_operation();
  2039. }
  2040. accept_event();
  2041. } else {
  2042. break;
  2043. }
  2044. }
  2045. return;
  2046. } break;
  2047. }
  2048. }
  2049. void TextEdit::_pre_shift_selection() {
  2050. if (!selection.active || selection.selecting_mode==Selection::MODE_NONE) {
  2051. selection.selecting_line=cursor.line;
  2052. selection.selecting_column=cursor.column;
  2053. selection.active=true;
  2054. }
  2055. selection.selecting_mode=Selection::MODE_SHIFT;
  2056. }
  2057. void TextEdit::_post_shift_selection() {
  2058. if (selection.active && selection.selecting_mode==Selection::MODE_SHIFT) {
  2059. select(selection.selecting_line,selection.selecting_column,cursor.line,cursor.column);
  2060. update();
  2061. }
  2062. selection.selecting_text=true;
  2063. }
  2064. void TextEdit::_scroll_lines_up() {
  2065. // adjust the vertical scroll
  2066. if (get_v_scroll() > 0) {
  2067. set_v_scroll(get_v_scroll() - 1);
  2068. }
  2069. // adjust the cursor
  2070. if (cursor_get_line() >= (get_visible_rows() + get_v_scroll()) && !selection.active) {
  2071. cursor_set_line((get_visible_rows() + get_v_scroll()) - 1, false);
  2072. }
  2073. }
  2074. void TextEdit::_scroll_lines_down() {
  2075. // calculate the maximum vertical scroll position
  2076. int max_v_scroll = get_line_count() - 1;
  2077. if (!scroll_past_end_of_file_enabled) {
  2078. max_v_scroll -= get_visible_rows() - 1;
  2079. }
  2080. // adjust the vertical scroll
  2081. if (get_v_scroll() < max_v_scroll) {
  2082. set_v_scroll(get_v_scroll() + 1);
  2083. }
  2084. // adjust the cursor
  2085. if ((cursor_get_line()) <= get_v_scroll() - 1 && !selection.active) {
  2086. cursor_set_line(get_v_scroll(), false);
  2087. }
  2088. }
  2089. /**** TEXT EDIT CORE API ****/
  2090. void TextEdit::_base_insert_text(int p_line, int p_char,const String& p_text,int &r_end_line,int &r_end_column) {
  2091. //save for undo...
  2092. ERR_FAIL_INDEX(p_line,text.size());
  2093. ERR_FAIL_COND(p_char<0);
  2094. /* STEP 1 add spaces if the char is greater than the end of the line */
  2095. while(p_char>text[p_line].length()) {
  2096. text.set(p_line,text[p_line]+String::chr(' '));
  2097. }
  2098. /* STEP 2 separate dest string in pre and post text */
  2099. String preinsert_text = text[p_line].substr(0,p_char);
  2100. String postinsert_text = text[p_line].substr(p_char,text[p_line].size());
  2101. /* STEP 3 remove \r from source text and separate in substrings */
  2102. //buh bye \r and split
  2103. Vector<String> substrings = p_text.replace("\r","").split("\n");
  2104. for(int i=0;i<substrings.size();i++) {
  2105. //insert the substrings
  2106. if (i==0) {
  2107. text.set(p_line,preinsert_text+substrings[i]);
  2108. } else {
  2109. text.insert(p_line+i,substrings[i]);
  2110. }
  2111. if (i==substrings.size()-1){
  2112. text.set(p_line+i,text[p_line+i]+postinsert_text);
  2113. }
  2114. }
  2115. r_end_line=p_line+substrings.size()-1;
  2116. r_end_column=text[r_end_line].length()-postinsert_text.length();
  2117. if (!text_changed_dirty && !setting_text) {
  2118. if (is_inside_tree())
  2119. MessageQueue::get_singleton()->push_call(this,"_text_changed_emit");
  2120. text_changed_dirty=true;
  2121. }
  2122. }
  2123. String TextEdit::_base_get_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) const {
  2124. ERR_FAIL_INDEX_V(p_from_line,text.size(),String());
  2125. ERR_FAIL_INDEX_V(p_from_column,text[p_from_line].length()+1,String());
  2126. ERR_FAIL_INDEX_V(p_to_line,text.size(),String());
  2127. ERR_FAIL_INDEX_V(p_to_column,text[p_to_line].length()+1,String());
  2128. ERR_FAIL_COND_V(p_to_line < p_from_line ,String()); // from > to
  2129. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column<p_from_column,String()); // from > to
  2130. String ret;
  2131. for(int i=p_from_line;i<=p_to_line;i++) {
  2132. int begin = (i==p_from_line)?p_from_column:0;
  2133. int end = (i==p_to_line)?p_to_column:text[i].length();
  2134. if (i>p_from_line)
  2135. ret+="\n";
  2136. ret+=text[i].substr(begin,end-begin);
  2137. }
  2138. return ret;
  2139. }
  2140. void TextEdit::_base_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) {
  2141. ERR_FAIL_INDEX(p_from_line,text.size());
  2142. ERR_FAIL_INDEX(p_from_column,text[p_from_line].length()+1);
  2143. ERR_FAIL_INDEX(p_to_line,text.size());
  2144. ERR_FAIL_INDEX(p_to_column,text[p_to_line].length()+1);
  2145. ERR_FAIL_COND(p_to_line < p_from_line ); // from > to
  2146. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column<p_from_column); // from > to
  2147. String pre_text = text[p_from_line].substr(0,p_from_column);
  2148. String post_text = text[p_to_line].substr(p_to_column,text[p_to_line].length());
  2149. for(int i=p_from_line;i<p_to_line;i++) {
  2150. text.remove(p_from_line+1);
  2151. }
  2152. text.set(p_from_line,pre_text+post_text);
  2153. if (!text_changed_dirty && !setting_text) {
  2154. if (is_inside_tree())
  2155. MessageQueue::get_singleton()->push_call(this,"_text_changed_emit");
  2156. text_changed_dirty=true;
  2157. }
  2158. }
  2159. void TextEdit::_insert_text(int p_line, int p_char,const String& p_text,int *r_end_line,int *r_end_column) {
  2160. if (!setting_text)
  2161. idle_detect->start();
  2162. if (undo_enabled) {
  2163. _clear_redo();
  2164. }
  2165. int retline,retchar;
  2166. _base_insert_text(p_line,p_char,p_text,retline,retchar);
  2167. if (r_end_line)
  2168. *r_end_line=retline;
  2169. if (r_end_column)
  2170. *r_end_column=retchar;
  2171. if (!undo_enabled)
  2172. return;
  2173. /* UNDO!! */
  2174. TextOperation op;
  2175. op.type=TextOperation::TYPE_INSERT;
  2176. op.from_line=p_line;
  2177. op.from_column=p_char;
  2178. op.to_line=retline;
  2179. op.to_column=retchar;
  2180. op.text=p_text;
  2181. op.version=++version;
  2182. op.chain_forward=false;
  2183. op.chain_backward=false;
  2184. //see if it shold just be set as current op
  2185. if (current_op.type!=op.type) {
  2186. op.prev_version = get_version();
  2187. _push_current_op();
  2188. current_op=op;
  2189. return; //set as current op, return
  2190. }
  2191. //see if it can be merged
  2192. if (current_op.to_line!=p_line || current_op.to_column!=p_char) {
  2193. op.prev_version = get_version();
  2194. _push_current_op();
  2195. current_op=op;
  2196. return; //set as current op, return
  2197. }
  2198. //merge current op
  2199. current_op.text+=p_text;
  2200. current_op.to_column=retchar;
  2201. current_op.to_line=retline;
  2202. current_op.version=op.version;
  2203. }
  2204. void TextEdit::_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) {
  2205. if (!setting_text)
  2206. idle_detect->start();
  2207. String text;
  2208. if (undo_enabled) {
  2209. _clear_redo();
  2210. text=_base_get_text(p_from_line,p_from_column,p_to_line,p_to_column);
  2211. }
  2212. _base_remove_text(p_from_line,p_from_column,p_to_line,p_to_column);
  2213. if (!undo_enabled)
  2214. return;
  2215. /* UNDO!! */
  2216. TextOperation op;
  2217. op.type=TextOperation::TYPE_REMOVE;
  2218. op.from_line=p_from_line;
  2219. op.from_column=p_from_column;
  2220. op.to_line=p_to_line;
  2221. op.to_column=p_to_column;
  2222. op.text=text;
  2223. op.version=++version;
  2224. op.chain_forward=false;
  2225. op.chain_backward=false;
  2226. //see if it shold just be set as current op
  2227. if (current_op.type!=op.type) {
  2228. op.prev_version = get_version();
  2229. _push_current_op();
  2230. current_op=op;
  2231. return; //set as current op, return
  2232. }
  2233. //see if it can be merged
  2234. if (current_op.from_line==p_to_line && current_op.from_column==p_to_column) {
  2235. //basckace or similar
  2236. current_op.text=text+current_op.text;
  2237. current_op.from_line=p_from_line;
  2238. current_op.from_column=p_from_column;
  2239. return; //update current op
  2240. }
  2241. if (current_op.from_line==p_from_line && current_op.from_column==p_from_column) {
  2242. //current_op.text=text+current_op.text;
  2243. //current_op.from_line=p_from_line;
  2244. //current_op.from_column=p_from_column;
  2245. //return; //update current op
  2246. }
  2247. op.prev_version = get_version();
  2248. _push_current_op();
  2249. current_op=op;
  2250. }
  2251. void TextEdit::_insert_text_at_cursor(const String& p_text) {
  2252. int new_column,new_line;
  2253. _insert_text(cursor.line,cursor.column,p_text,&new_line,&new_column);
  2254. cursor_set_line(new_line);
  2255. cursor_set_column(new_column);
  2256. update();
  2257. }
  2258. int TextEdit::get_char_count() {
  2259. int totalsize=0;
  2260. for (int i=0;i<text.size();i++) {
  2261. if (i>0)
  2262. totalsize++; // incliude \n
  2263. totalsize+=text[i].length();
  2264. }
  2265. return totalsize; // omit last \n
  2266. }
  2267. Size2 TextEdit::get_minimum_size() const {
  2268. return cache.style_normal->get_minimum_size();
  2269. }
  2270. int TextEdit::get_visible_rows() const {
  2271. int total=cache.size.height;
  2272. total-=cache.style_normal->get_minimum_size().height;
  2273. total/=get_row_height();
  2274. return total;
  2275. }
  2276. void TextEdit::adjust_viewport_to_cursor() {
  2277. if (cursor.line_ofs>cursor.line)
  2278. cursor.line_ofs=cursor.line;
  2279. int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w-cache.breakpoint_gutter_width;
  2280. if (v_scroll->is_visible())
  2281. visible_width-=v_scroll->get_combined_minimum_size().width;
  2282. visible_width-=20; // give it a little more space
  2283. //printf("rowofs %i, visrows %i, cursor.line %i\n",cursor.line_ofs,get_visible_rows(),cursor.line);
  2284. int visible_rows = get_visible_rows();
  2285. if (h_scroll->is_visible())
  2286. visible_rows-=((h_scroll->get_combined_minimum_size().height-1)/get_row_height());
  2287. if (cursor.line>=(cursor.line_ofs+visible_rows))
  2288. cursor.line_ofs=cursor.line-visible_rows+1;
  2289. if (cursor.line<cursor.line_ofs)
  2290. cursor.line_ofs=cursor.line;
  2291. int cursor_x = get_column_x_offset( cursor.column, text[cursor.line] );
  2292. if (cursor_x>(cursor.x_ofs+visible_width))
  2293. cursor.x_ofs=cursor_x-visible_width+1;
  2294. if (cursor_x < cursor.x_ofs)
  2295. cursor.x_ofs=cursor_x;
  2296. update();
  2297. /*
  2298. get_range()->set_max(text.size());
  2299. get_range()->set_page(get_visible_rows());
  2300. get_range()->set((int)cursor.line_ofs);
  2301. */
  2302. }
  2303. void TextEdit::center_viewport_to_cursor() {
  2304. if (cursor.line_ofs>cursor.line)
  2305. cursor.line_ofs=cursor.line;
  2306. int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w-cache.breakpoint_gutter_width;
  2307. if (v_scroll->is_visible())
  2308. visible_width-=v_scroll->get_combined_minimum_size().width;
  2309. visible_width-=20; // give it a little more space
  2310. int visible_rows = get_visible_rows();
  2311. if (h_scroll->is_visible())
  2312. visible_rows-=((h_scroll->get_combined_minimum_size().height-1)/get_row_height());
  2313. int max_ofs = text.size()-(scroll_past_end_of_file_enabled?1:visible_rows);
  2314. cursor.line_ofs=CLAMP(cursor.line-(visible_rows/2),0,max_ofs);
  2315. int cursor_x = get_column_x_offset( cursor.column, text[cursor.line] );
  2316. if (cursor_x>(cursor.x_ofs+visible_width))
  2317. cursor.x_ofs=cursor_x-visible_width+1;
  2318. if (cursor_x < cursor.x_ofs)
  2319. cursor.x_ofs=cursor_x;
  2320. update();
  2321. }
  2322. void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
  2323. if (p_col<0)
  2324. p_col=0;
  2325. cursor.column=p_col;
  2326. if (cursor.column > get_line( cursor.line ).length())
  2327. cursor.column=get_line( cursor.line ).length();
  2328. cursor.last_fit_x=get_column_x_offset(cursor.column,get_line(cursor.line));
  2329. if (p_adjust_viewport)
  2330. adjust_viewport_to_cursor();
  2331. if (!cursor_changed_dirty) {
  2332. if (is_inside_tree())
  2333. MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit");
  2334. cursor_changed_dirty=true;
  2335. }
  2336. }
  2337. void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport) {
  2338. if (setting_row)
  2339. return;
  2340. setting_row=true;
  2341. if (p_row<0)
  2342. p_row=0;
  2343. if (p_row>=(int)text.size())
  2344. p_row=(int)text.size()-1;
  2345. cursor.line=p_row;
  2346. cursor.column=get_char_pos_for( cursor.last_fit_x, get_line( cursor.line) );
  2347. if (p_adjust_viewport)
  2348. adjust_viewport_to_cursor();
  2349. setting_row=false;
  2350. if (!cursor_changed_dirty) {
  2351. if (is_inside_tree())
  2352. MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit");
  2353. cursor_changed_dirty=true;
  2354. }
  2355. }
  2356. int TextEdit::cursor_get_column() const {
  2357. return cursor.column;
  2358. }
  2359. int TextEdit::cursor_get_line() const {
  2360. return cursor.line;
  2361. }
  2362. bool TextEdit::cursor_get_blink_enabled() const {
  2363. return caret_blink_enabled;
  2364. }
  2365. void TextEdit::cursor_set_blink_enabled(const bool p_enabled) {
  2366. caret_blink_enabled = p_enabled;
  2367. if (p_enabled) {
  2368. caret_blink_timer->start();
  2369. } else {
  2370. caret_blink_timer->stop();
  2371. }
  2372. draw_caret = true;
  2373. }
  2374. float TextEdit::cursor_get_blink_speed() const {
  2375. return caret_blink_timer->get_wait_time();
  2376. }
  2377. void TextEdit::cursor_set_blink_speed(const float p_speed) {
  2378. ERR_FAIL_COND(p_speed <= 0);
  2379. caret_blink_timer->set_wait_time(p_speed);
  2380. }
  2381. void TextEdit::cursor_set_block_mode(const bool p_enable){
  2382. block_caret = p_enable;
  2383. update();
  2384. }
  2385. bool TextEdit::cursor_is_block_mode() const {
  2386. return block_caret;
  2387. }
  2388. void TextEdit::_scroll_moved(double p_to_val) {
  2389. if (updating_scrolls)
  2390. return;
  2391. if (h_scroll->is_visible())
  2392. cursor.x_ofs=h_scroll->get_val();
  2393. if (v_scroll->is_visible())
  2394. cursor.line_ofs=v_scroll->get_val();
  2395. update();
  2396. }
  2397. int TextEdit::get_row_height() const {
  2398. return cache.font->get_height()+cache.line_spacing;
  2399. }
  2400. int TextEdit::get_char_pos_for(int p_px,String p_str) const {
  2401. int px=0;
  2402. int c=0;
  2403. int tab_w = cache.font->get_char_size(' ').width*tab_size;
  2404. while (c<p_str.length()) {
  2405. int w=0;
  2406. if (p_str[c]=='\t') {
  2407. int left = px%tab_w;
  2408. if (left==0)
  2409. w=tab_w;
  2410. else
  2411. w=tab_w-px%tab_w; // is right...
  2412. } else {
  2413. w=cache.font->get_char_size(p_str[c],p_str[c+1]).width;
  2414. }
  2415. if (p_px<(px+w/2))
  2416. break;
  2417. px+=w;
  2418. c++;
  2419. }
  2420. return c;
  2421. }
  2422. int TextEdit::get_column_x_offset(int p_char,String p_str) {
  2423. int px=0;
  2424. int tab_w = cache.font->get_char_size(' ').width*tab_size;
  2425. for (int i=0;i<p_char;i++) {
  2426. if (i>=p_str.length())
  2427. break;
  2428. if (p_str[i]=='\t') {
  2429. int left = px%tab_w;
  2430. if (left==0)
  2431. px+=tab_w;
  2432. else
  2433. px+=tab_w-px%tab_w; // is right...
  2434. } else {
  2435. px+=cache.font->get_char_size(p_str[i],p_str[i+1]).width;
  2436. }
  2437. }
  2438. return px;
  2439. }
  2440. void TextEdit::insert_text_at_cursor(const String& p_text) {
  2441. if (selection.active) {
  2442. cursor_set_line(selection.from_line);
  2443. cursor_set_column(selection.from_column);
  2444. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2445. selection.active=false;
  2446. selection.selecting_mode=Selection::MODE_NONE;
  2447. }
  2448. _insert_text_at_cursor(p_text);
  2449. update();
  2450. }
  2451. Control::CursorShape TextEdit::get_cursor_shape(const Point2& p_pos) const {
  2452. if (hilighted_word!=String())
  2453. return CURSOR_POINTING_HAND;
  2454. int gutter=cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width;
  2455. if((completion_active && completion_rect.has_point(p_pos)) || p_pos.x < gutter) {
  2456. return CURSOR_ARROW;
  2457. }
  2458. return CURSOR_IBEAM;
  2459. }
  2460. void TextEdit::set_text(String p_text){
  2461. setting_text=true;
  2462. clear();
  2463. _insert_text_at_cursor(p_text);
  2464. clear_undo_history();
  2465. cursor.column=0;
  2466. cursor.line=0;
  2467. cursor.x_ofs=0;
  2468. cursor.line_ofs=0;
  2469. cursor.last_fit_x=0;
  2470. cursor_set_line(0);
  2471. cursor_set_column(0);
  2472. update();
  2473. setting_text=false;
  2474. //get_range()->set(0);
  2475. };
  2476. String TextEdit::get_text() {
  2477. String longthing;
  2478. int len = text.size();
  2479. for (int i=0;i<len;i++) {
  2480. longthing+=text[i];
  2481. if (i!=len-1)
  2482. longthing+="\n";
  2483. }
  2484. return longthing;
  2485. };
  2486. String TextEdit::get_text_for_lookup_completion() {
  2487. int row,col;
  2488. _get_mouse_pos(get_local_mouse_pos(), row,col);
  2489. String longthing;
  2490. int len = text.size();
  2491. for (int i=0;i<len;i++) {
  2492. if (i==row) {
  2493. longthing+=text[i].substr(0,col);
  2494. longthing+=String::chr(0xFFFF); //not unicode, represents the cursor
  2495. longthing+=text[i].substr(col,text[i].size());
  2496. } else {
  2497. longthing+=text[i];
  2498. }
  2499. if (i!=len-1)
  2500. longthing+="\n";
  2501. }
  2502. return longthing;
  2503. }
  2504. String TextEdit::get_text_for_completion() {
  2505. String longthing;
  2506. int len = text.size();
  2507. for (int i=0;i<len;i++) {
  2508. if (i==cursor.line) {
  2509. longthing+=text[i].substr(0,cursor.column);
  2510. longthing+=String::chr(0xFFFF); //not unicode, represents the cursor
  2511. longthing+=text[i].substr(cursor.column,text[i].size());
  2512. } else {
  2513. longthing+=text[i];
  2514. }
  2515. if (i!=len-1)
  2516. longthing+="\n";
  2517. }
  2518. return longthing;
  2519. };
  2520. String TextEdit::get_line(int line) const {
  2521. if (line<0 || line>=text.size())
  2522. return "";
  2523. return text[line];
  2524. };
  2525. void TextEdit::_clear() {
  2526. clear_undo_history();
  2527. text.clear();
  2528. cursor.column=0;
  2529. cursor.line=0;
  2530. cursor.x_ofs=0;
  2531. cursor.line_ofs=0;
  2532. cursor.last_fit_x=0;
  2533. }
  2534. void TextEdit::clear() {
  2535. setting_text=true;
  2536. _clear();
  2537. setting_text=false;
  2538. };
  2539. void TextEdit::set_readonly(bool p_readonly) {
  2540. readonly=p_readonly;
  2541. }
  2542. void TextEdit::set_wrap(bool p_wrap) {
  2543. wrap=p_wrap;
  2544. }
  2545. void TextEdit::set_max_chars(int p_max_chars) {
  2546. max_chars=p_max_chars;
  2547. }
  2548. void TextEdit::_reset_caret_blink_timer() {
  2549. if (caret_blink_enabled) {
  2550. caret_blink_timer->stop();
  2551. caret_blink_timer->start();
  2552. draw_caret = true;
  2553. update();
  2554. }
  2555. }
  2556. void TextEdit::_toggle_draw_caret() {
  2557. draw_caret = !draw_caret;
  2558. if (is_visible()) {
  2559. update();
  2560. }
  2561. }
  2562. void TextEdit::_update_caches() {
  2563. cache.style_normal=get_stylebox("normal");
  2564. cache.style_focus=get_stylebox("focus");
  2565. cache.completion_background_color=get_color("completion_background_color");
  2566. cache.completion_selected_color=get_color("completion_selected_color");
  2567. cache.completion_existing_color=get_color("completion_existing_color");
  2568. cache.completion_font_color=get_color("completion_font_color");
  2569. cache.font=get_font("font");
  2570. cache.caret_color=get_color("caret_color");
  2571. cache.caret_background_color=get_color("caret_background_color");
  2572. cache.line_number_color=get_color("line_number_color");
  2573. cache.font_color=get_color("font_color");
  2574. cache.font_selected_color=get_color("font_selected_color");
  2575. cache.keyword_color=get_color("keyword_color");
  2576. cache.function_color=get_color("function_color");
  2577. cache.member_variable_color=get_color("member_variable_color");
  2578. cache.number_color=get_color("number_color");
  2579. cache.selection_color=get_color("selection_color");
  2580. cache.mark_color=get_color("mark_color");
  2581. cache.current_line_color=get_color("current_line_color");
  2582. cache.breakpoint_color=get_color("breakpoint_color");
  2583. cache.brace_mismatch_color=get_color("brace_mismatch_color");
  2584. cache.word_highlighted_color=get_color("word_highlighted_color");
  2585. cache.search_result_color=get_color("search_result_color");
  2586. cache.search_result_border_color=get_color("search_result_border_color");
  2587. cache.line_spacing=get_constant("line_spacing");
  2588. cache.row_height = cache.font->get_height() + cache.line_spacing;
  2589. cache.tab_icon=get_icon("tab");
  2590. text.set_font(cache.font);
  2591. }
  2592. void TextEdit::clear_colors() {
  2593. keywords.clear();
  2594. color_regions.clear();;
  2595. text.clear_caches();
  2596. custom_bg_color=Color(0,0,0,0);
  2597. }
  2598. void TextEdit::set_custom_bg_color(const Color& p_color) {
  2599. custom_bg_color=p_color;
  2600. update();
  2601. }
  2602. void TextEdit::add_keyword_color(const String& p_keyword,const Color& p_color) {
  2603. keywords[p_keyword]=p_color;
  2604. update();
  2605. }
  2606. void TextEdit::add_color_region(const String& p_begin_key,const String& p_end_key,const Color &p_color,bool p_line_only) {
  2607. color_regions.push_back(ColorRegion(p_begin_key,p_end_key,p_color,p_line_only));
  2608. text.clear_caches();
  2609. update();
  2610. }
  2611. void TextEdit::set_symbol_color(const Color& p_color) {
  2612. symbol_color=p_color;
  2613. update();
  2614. }
  2615. void TextEdit::set_syntax_coloring(bool p_enabled) {
  2616. syntax_coloring=p_enabled;
  2617. update();
  2618. }
  2619. bool TextEdit::is_syntax_coloring_enabled() const {
  2620. return syntax_coloring;
  2621. }
  2622. void TextEdit::set_auto_indent(bool p_auto_indent) {
  2623. auto_indent = p_auto_indent;
  2624. }
  2625. void TextEdit::cut() {
  2626. if (!selection.active) {
  2627. String clipboard = text[cursor.line];
  2628. OS::get_singleton()->set_clipboard(clipboard);
  2629. cursor_set_line(cursor.line);
  2630. cursor_set_column(0);
  2631. _remove_text(cursor.line,0,cursor.line,text[cursor.line].length());
  2632. backspace_at_cursor();
  2633. update();
  2634. cursor_set_line(cursor.line+1);
  2635. cut_copy_line = true;
  2636. } else {
  2637. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2638. OS::get_singleton()->set_clipboard(clipboard);
  2639. cursor_set_line(selection.from_line);
  2640. cursor_set_column(selection.from_column);
  2641. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2642. selection.active=false;
  2643. selection.selecting_mode=Selection::MODE_NONE;
  2644. update();
  2645. cut_copy_line = false;
  2646. }
  2647. }
  2648. void TextEdit::copy() {
  2649. if (!selection.active)
  2650. return;
  2651. if (!selection.active) {
  2652. String clipboard = _base_get_text(cursor.line,0,cursor.line,text[cursor.line].length());
  2653. OS::get_singleton()->set_clipboard(clipboard);
  2654. cut_copy_line = true;
  2655. } else {
  2656. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2657. OS::get_singleton()->set_clipboard(clipboard);
  2658. cut_copy_line = false;
  2659. }
  2660. }
  2661. void TextEdit::paste() {
  2662. String clipboard = OS::get_singleton()->get_clipboard();
  2663. if (selection.active) {
  2664. selection.active=false;
  2665. selection.selecting_mode=Selection::MODE_NONE;
  2666. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2667. cursor_set_line(selection.from_line);
  2668. cursor_set_column(selection.from_column);
  2669. } else if (cut_copy_line) {
  2670. cursor_set_column(0);
  2671. String ins="\n";
  2672. clipboard += ins;
  2673. }
  2674. _insert_text_at_cursor(clipboard);
  2675. update();
  2676. }
  2677. void TextEdit::select_all() {
  2678. if (text.size()==1 && text[0].length()==0)
  2679. return;
  2680. selection.active=true;
  2681. selection.from_line=0;
  2682. selection.from_column=0;
  2683. selection.selecting_line=0;
  2684. selection.selecting_column=0;
  2685. selection.to_line=text.size()-1;
  2686. selection.to_column=text[selection.to_line].length();
  2687. selection.selecting_mode=Selection::MODE_SHIFT;
  2688. selection.shiftclick_left=true;
  2689. cursor_set_line( selection.to_line, false );
  2690. cursor_set_column( selection.to_column, false );
  2691. update();
  2692. }
  2693. void TextEdit::deselect() {
  2694. selection.active=false;
  2695. update();
  2696. }
  2697. void TextEdit::select(int p_from_line,int p_from_column,int p_to_line,int p_to_column) {
  2698. if (p_from_line>=text.size())
  2699. p_from_line=text.size()-1;
  2700. if (p_from_column>=text[p_from_line].length())
  2701. p_from_column=text[p_from_line].length();
  2702. if (p_to_line>=text.size())
  2703. p_to_line=text.size()-1;
  2704. if (p_to_column>=text[p_to_line].length())
  2705. p_to_column=text[p_to_line].length();
  2706. selection.from_line=p_from_line;
  2707. selection.from_column=p_from_column;
  2708. selection.to_line=p_to_line;
  2709. selection.to_column=p_to_column;
  2710. selection.active=true;
  2711. if (selection.from_line==selection.to_line) {
  2712. if (selection.from_column==selection.to_column) {
  2713. selection.active=false;
  2714. } else if (selection.from_column>selection.to_column) {
  2715. selection.shiftclick_left = false;
  2716. SWAP( selection.from_column, selection.to_column );
  2717. } else {
  2718. selection.shiftclick_left = true;
  2719. }
  2720. } else if (selection.from_line>selection.to_line) {
  2721. selection.shiftclick_left = false;
  2722. SWAP( selection.from_line, selection.to_line );
  2723. SWAP( selection.from_column, selection.to_column );
  2724. } else {
  2725. selection.shiftclick_left = true;
  2726. }
  2727. update();
  2728. }
  2729. bool TextEdit::is_selection_active() const {
  2730. return selection.active;
  2731. }
  2732. int TextEdit::get_selection_from_line() const {
  2733. ERR_FAIL_COND_V(!selection.active,-1);
  2734. return selection.from_line;
  2735. }
  2736. int TextEdit::get_selection_from_column() const {
  2737. ERR_FAIL_COND_V(!selection.active,-1);
  2738. return selection.from_column;
  2739. }
  2740. int TextEdit::get_selection_to_line() const {
  2741. ERR_FAIL_COND_V(!selection.active,-1);
  2742. return selection.to_line;
  2743. }
  2744. int TextEdit::get_selection_to_column() const {
  2745. ERR_FAIL_COND_V(!selection.active,-1);
  2746. return selection.to_column;
  2747. }
  2748. String TextEdit::get_selection_text() const {
  2749. if (!selection.active)
  2750. return "";
  2751. return _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2752. }
  2753. String TextEdit::get_word_under_cursor() const {
  2754. int prev_cc = cursor.column;
  2755. while(prev_cc >0) {
  2756. bool is_char = _is_text_char(text[cursor.line][prev_cc-1]);
  2757. if (!is_char)
  2758. break;
  2759. --prev_cc;
  2760. }
  2761. int next_cc = cursor.column;
  2762. while(next_cc<text[cursor.line].length()) {
  2763. bool is_char = _is_text_char(text[cursor.line][next_cc]);
  2764. if(!is_char)
  2765. break;
  2766. ++ next_cc;
  2767. }
  2768. if (prev_cc == cursor.column || next_cc == cursor.column)
  2769. return "";
  2770. return text[cursor.line].substr(prev_cc, next_cc-prev_cc);
  2771. }
  2772. void TextEdit::set_search_text(const String &p_search_text) {
  2773. search_text = p_search_text;
  2774. }
  2775. void TextEdit::set_search_flags(uint32_t p_flags) {
  2776. search_flags = p_flags;
  2777. }
  2778. void TextEdit::set_current_search_result(int line, int col) {
  2779. search_result_line = line;
  2780. search_result_col = col;
  2781. update();
  2782. }
  2783. void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
  2784. highlight_all_occurrences = p_enabled;
  2785. update();
  2786. }
  2787. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  2788. return highlight_all_occurrences;
  2789. }
  2790. int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) {
  2791. int col = -1;
  2792. if (p_key.length() > 0 && p_search.length() > 0) {
  2793. if (p_from_column < 0 || p_from_column > p_search.length()) {
  2794. p_from_column = 0;
  2795. }
  2796. while (col == -1 && p_from_column <= p_search.length()) {
  2797. if (p_search_flags&SEARCH_MATCH_CASE) {
  2798. col = p_search.find(p_key,p_from_column);
  2799. } else {
  2800. col = p_search.findn(p_key,p_from_column);
  2801. }
  2802. // whole words only
  2803. if (col != -1 && p_search_flags&SEARCH_WHOLE_WORDS) {
  2804. p_from_column=col;
  2805. if (col > 0 && _is_text_char(p_search[col-1])) {
  2806. col = -1;
  2807. } else if (_is_text_char(p_search[col+p_key.length()])) {
  2808. col = -1;
  2809. }
  2810. }
  2811. p_from_column+=1;
  2812. }
  2813. }
  2814. return col;
  2815. }
  2816. DVector<int> TextEdit::_search_bind(const String &p_key,uint32_t p_search_flags, int p_from_line,int p_from_column) const {
  2817. int col,line;
  2818. if (search(p_key,p_search_flags,p_from_line,p_from_column,col,line)) {
  2819. DVector<int> result;
  2820. result.resize(2);
  2821. result.set(0,line);
  2822. result.set(1,col);
  2823. return result;
  2824. } else {
  2825. return DVector<int>();
  2826. }
  2827. }
  2828. 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 {
  2829. if (p_key.length()==0)
  2830. return false;
  2831. ERR_FAIL_INDEX_V(p_from_line,text.size(),false);
  2832. ERR_FAIL_INDEX_V(p_from_column,text[p_from_line].length()+1,false);
  2833. //search through the whole documment, but start by current line
  2834. int line=-1;
  2835. int pos=-1;
  2836. line=p_from_line;
  2837. for(int i=0;i<text.size()+1;i++) {
  2838. //backwards is broken...
  2839. //int idx=(p_search_flags&SEARCH_BACKWARDS)?(text.size()-i):i; //do backwards seearch
  2840. if (line<0) {
  2841. line=text.size()-1;
  2842. }
  2843. if (line==text.size()) {
  2844. line=0;
  2845. }
  2846. String text_line = text[line];
  2847. int from_column=0;
  2848. if (line==p_from_line) {
  2849. if (i==text.size()) {
  2850. //wrapped
  2851. if (p_search_flags&SEARCH_BACKWARDS) {
  2852. from_column=text_line.length();
  2853. } else {
  2854. from_column=0;
  2855. }
  2856. } else {
  2857. from_column=p_from_column;
  2858. }
  2859. } else {
  2860. if (p_search_flags&SEARCH_BACKWARDS)
  2861. from_column=text_line.length()-1;
  2862. else
  2863. from_column=0;
  2864. }
  2865. pos=-1;
  2866. int pos_from=0;
  2867. int last_pos=-1;
  2868. while ((last_pos=(p_search_flags&SEARCH_MATCH_CASE)?text_line.find(p_key,pos_from):text_line.findn(p_key,pos_from))!=-1) {
  2869. if (p_search_flags&SEARCH_BACKWARDS) {
  2870. if (last_pos>from_column)
  2871. break;
  2872. pos=last_pos;
  2873. } else {
  2874. if (last_pos>=from_column) {
  2875. pos=last_pos;
  2876. break;
  2877. }
  2878. }
  2879. pos_from=last_pos+p_key.length();
  2880. }
  2881. if (pos!=-1 && (p_search_flags&SEARCH_WHOLE_WORDS)) {
  2882. //validate for whole words
  2883. if (pos>0 && _is_text_char(text_line[pos-1]))
  2884. pos=-1;
  2885. else if (_is_text_char(text_line[pos+p_key.length()]))
  2886. pos=-1;
  2887. }
  2888. if (pos!=-1)
  2889. break;
  2890. if (p_search_flags&SEARCH_BACKWARDS)
  2891. line--;
  2892. else
  2893. line++;
  2894. }
  2895. if (pos==-1) {
  2896. r_line=-1;
  2897. r_column=-1;
  2898. return false;
  2899. }
  2900. r_line=line;
  2901. r_column=pos;
  2902. return true;
  2903. }
  2904. void TextEdit::_cursor_changed_emit() {
  2905. emit_signal("cursor_changed");
  2906. cursor_changed_dirty=false;
  2907. }
  2908. void TextEdit::_text_changed_emit() {
  2909. emit_signal("text_changed");
  2910. text_changed_dirty=false;
  2911. }
  2912. void TextEdit::set_line_as_marked(int p_line,bool p_marked) {
  2913. ERR_FAIL_INDEX(p_line,text.size());
  2914. text.set_marked(p_line,p_marked);
  2915. update();
  2916. }
  2917. bool TextEdit::is_line_set_as_breakpoint(int p_line) const {
  2918. ERR_FAIL_INDEX_V(p_line,text.size(),false);
  2919. return text.is_breakpoint(p_line);
  2920. }
  2921. void TextEdit::set_line_as_breakpoint(int p_line,bool p_breakpoint) {
  2922. ERR_FAIL_INDEX(p_line,text.size());
  2923. text.set_breakpoint(p_line,p_breakpoint);
  2924. update();
  2925. }
  2926. void TextEdit::get_breakpoints(List<int> *p_breakpoints) const {
  2927. for(int i=0;i<text.size();i++) {
  2928. if (text.is_breakpoint(i))
  2929. p_breakpoints->push_back(i);
  2930. }
  2931. }
  2932. int TextEdit::get_line_count() const {
  2933. return text.size();
  2934. }
  2935. void TextEdit::_do_text_op(const TextOperation& p_op, bool p_reverse) {
  2936. ERR_FAIL_COND(p_op.type==TextOperation::TYPE_NONE);
  2937. bool insert = p_op.type==TextOperation::TYPE_INSERT;
  2938. if (p_reverse)
  2939. insert=!insert;
  2940. if (insert) {
  2941. int check_line;
  2942. int check_column;
  2943. _base_insert_text(p_op.from_line,p_op.from_column,p_op.text,check_line,check_column);
  2944. ERR_FAIL_COND( check_line != p_op.to_line ); // BUG
  2945. ERR_FAIL_COND( check_column != p_op.to_column ); // BUG
  2946. } else {
  2947. _base_remove_text(p_op.from_line,p_op.from_column,p_op.to_line,p_op.to_column);
  2948. }
  2949. }
  2950. void TextEdit::_clear_redo() {
  2951. if (undo_stack_pos==NULL)
  2952. return; //nothing to clear
  2953. _push_current_op();
  2954. while (undo_stack_pos) {
  2955. List<TextOperation>::Element *elem = undo_stack_pos;
  2956. undo_stack_pos=undo_stack_pos->next();
  2957. undo_stack.erase(elem);
  2958. }
  2959. }
  2960. void TextEdit::undo() {
  2961. _push_current_op();
  2962. if (undo_stack_pos==NULL) {
  2963. if (!undo_stack.size())
  2964. return; //nothing to undo
  2965. undo_stack_pos=undo_stack.back();
  2966. } else if (undo_stack_pos==undo_stack.front())
  2967. return; // at the bottom of the undo stack
  2968. else
  2969. undo_stack_pos=undo_stack_pos->prev();
  2970. TextOperation op = undo_stack_pos->get();
  2971. _do_text_op(op, true);
  2972. current_op.version=op.prev_version;
  2973. if(undo_stack_pos->get().chain_backward) {
  2974. while(true) {
  2975. ERR_BREAK(!undo_stack_pos->prev());
  2976. undo_stack_pos = undo_stack_pos->prev();
  2977. op = undo_stack_pos->get();
  2978. _do_text_op(op, true);
  2979. current_op.version = op.prev_version;
  2980. if (undo_stack_pos->get().chain_forward) {
  2981. break;
  2982. }
  2983. }
  2984. }
  2985. if (undo_stack_pos->get().type == TextOperation::TYPE_REMOVE) {
  2986. cursor_set_line(undo_stack_pos->get().to_line);
  2987. cursor_set_column(undo_stack_pos->get().to_column);
  2988. _cancel_code_hint();
  2989. } else {
  2990. cursor_set_line(undo_stack_pos->get().from_line);
  2991. cursor_set_column(undo_stack_pos->get().from_column);
  2992. }
  2993. update();
  2994. }
  2995. void TextEdit::redo() {
  2996. _push_current_op();
  2997. if (undo_stack_pos==NULL)
  2998. return; //nothing to do.
  2999. TextOperation op = undo_stack_pos->get();
  3000. _do_text_op(op, false);
  3001. current_op.version = op.version;
  3002. if(undo_stack_pos->get().chain_forward) {
  3003. while(true) {
  3004. ERR_BREAK(!undo_stack_pos->next());
  3005. undo_stack_pos=undo_stack_pos->next();
  3006. op = undo_stack_pos->get();
  3007. _do_text_op(op, false);
  3008. current_op.version = op.version;
  3009. if (undo_stack_pos->get().chain_backward)
  3010. break;
  3011. }
  3012. }
  3013. cursor_set_line(undo_stack_pos->get().to_line);
  3014. cursor_set_column(undo_stack_pos->get().to_column);
  3015. undo_stack_pos=undo_stack_pos->next();
  3016. update();
  3017. }
  3018. void TextEdit::clear_undo_history() {
  3019. saved_version=0;
  3020. current_op.type=TextOperation::TYPE_NONE;
  3021. undo_stack_pos=NULL;
  3022. undo_stack.clear();
  3023. }
  3024. void TextEdit::begin_complex_operation() {
  3025. _push_current_op();
  3026. next_operation_is_complex=true;
  3027. }
  3028. void TextEdit::end_complex_operation() {
  3029. _push_current_op();
  3030. ERR_FAIL_COND(undo_stack.size() == 0);
  3031. if(undo_stack.back()->get().chain_forward) {
  3032. undo_stack.back()->get().chain_forward=false;
  3033. return;
  3034. }
  3035. undo_stack.back()->get().chain_backward=true;
  3036. }
  3037. void TextEdit::_push_current_op() {
  3038. if (current_op.type==TextOperation::TYPE_NONE)
  3039. return; // do nothing
  3040. if(next_operation_is_complex) {
  3041. current_op.chain_forward=true;
  3042. next_operation_is_complex=false;
  3043. }
  3044. undo_stack.push_back(current_op);
  3045. current_op.type=TextOperation::TYPE_NONE;
  3046. current_op.text="";
  3047. current_op.chain_forward=false;
  3048. }
  3049. void TextEdit::set_tab_size(const int p_size) {
  3050. ERR_FAIL_COND(p_size <= 0);
  3051. tab_size = p_size;
  3052. text.set_tab_size(p_size);
  3053. update();
  3054. }
  3055. void TextEdit::set_draw_tabs(bool p_draw) {
  3056. draw_tabs=p_draw;
  3057. }
  3058. bool TextEdit::is_drawing_tabs() const{
  3059. return draw_tabs;
  3060. }
  3061. void TextEdit::set_insert_mode(bool p_enabled) {
  3062. insert_mode = p_enabled;
  3063. update();
  3064. }
  3065. bool TextEdit::is_insert_mode() const {
  3066. return insert_mode;
  3067. }
  3068. uint32_t TextEdit::get_version() const {
  3069. return current_op.version;
  3070. }
  3071. uint32_t TextEdit::get_saved_version() const {
  3072. return saved_version;
  3073. }
  3074. void TextEdit::tag_saved_version() {
  3075. saved_version=get_version();
  3076. }
  3077. int TextEdit::get_v_scroll() const {
  3078. return v_scroll->get_val();
  3079. }
  3080. void TextEdit::set_v_scroll(int p_scroll) {
  3081. v_scroll->set_val(p_scroll);
  3082. cursor.line_ofs=p_scroll;
  3083. }
  3084. int TextEdit::get_h_scroll() const {
  3085. return h_scroll->get_val();
  3086. }
  3087. void TextEdit::set_h_scroll(int p_scroll) {
  3088. h_scroll->set_val(p_scroll);
  3089. }
  3090. void TextEdit::set_completion(bool p_enabled,const Vector<String>& p_prefixes) {
  3091. completion_prefixes.clear();
  3092. completion_enabled=p_enabled;
  3093. for(int i=0;i<p_prefixes.size();i++)
  3094. completion_prefixes.insert(p_prefixes[i]);
  3095. }
  3096. void TextEdit::_confirm_completion() {
  3097. begin_complex_operation();
  3098. _remove_text(cursor.line, cursor.column - completion_base.length(), cursor.line, cursor.column);
  3099. cursor_set_column(cursor.column - completion_base.length(), false);
  3100. insert_text_at_cursor(completion_current);
  3101. if (completion_current.ends_with("(") && auto_brace_completion_enabled) {
  3102. insert_text_at_cursor(")");
  3103. cursor.column--;
  3104. }
  3105. end_complex_operation();
  3106. _cancel_completion();
  3107. }
  3108. void TextEdit::_cancel_code_hint() {
  3109. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
  3110. raised_from_completion = false;
  3111. completion_hint="";
  3112. update();
  3113. }
  3114. void TextEdit::_cancel_completion() {
  3115. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
  3116. raised_from_completion = false;
  3117. if (!completion_active)
  3118. return;
  3119. completion_active=false;
  3120. update();
  3121. }
  3122. static bool _is_completable(CharType c) {
  3123. return !_is_symbol(c) || c=='"' || c=='\'';
  3124. }
  3125. void TextEdit::_update_completion_candidates() {
  3126. String l = text[cursor.line];
  3127. int cofs = CLAMP(cursor.column,0,l.length());
  3128. String s;
  3129. //look for keywords first
  3130. bool inquote=false;
  3131. int first_quote=-1;
  3132. int c=cofs-1;
  3133. while(c>=0) {
  3134. if (l[c]=='"' || l[c]=='\'') {
  3135. inquote=!inquote;
  3136. if (first_quote==-1)
  3137. first_quote=c;
  3138. }
  3139. c--;
  3140. }
  3141. bool pre_keyword=false;
  3142. bool cancel=false;
  3143. //print_line("inquote: "+itos(inquote)+"first quote "+itos(first_quote)+" cofs-1 "+itos(cofs-1));
  3144. if (!inquote && first_quote==cofs-1) {
  3145. //no completion here
  3146. //print_line("cancel!");
  3147. cancel=true;
  3148. } if (inquote && first_quote!=-1) {
  3149. s=l.substr(first_quote,cofs-first_quote);
  3150. //print_line("s: 1"+s);
  3151. } else if (cofs>0 && l[cofs-1]==' ') {
  3152. int kofs=cofs-1;
  3153. String kw;
  3154. while (kofs>=0 && l[kofs]==' ')
  3155. kofs--;
  3156. while(kofs>=0 && l[kofs]>32 && _is_completable(l[kofs])) {
  3157. kw=String::chr(l[kofs])+kw;
  3158. kofs--;
  3159. }
  3160. pre_keyword=keywords.has(kw);
  3161. //print_line("KW "+kw+"? "+itos(pre_keyword));
  3162. } else {
  3163. while(cofs>0 && l[cofs-1]>32 && _is_completable(l[cofs-1])) {
  3164. s=String::chr(l[cofs-1])+s;
  3165. if (l[cofs-1]=='\'' || l[cofs-1]=='"')
  3166. break;
  3167. cofs--;
  3168. }
  3169. }
  3170. if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_strings[0].begins_with("\"")) {
  3171. cancel = true;
  3172. }
  3173. update();
  3174. if (cancel || (!pre_keyword && s=="" && (cofs==0 || !completion_prefixes.has(String::chr(l[cofs-1]))))) {
  3175. //none to complete, cancel
  3176. _cancel_completion();
  3177. return;
  3178. }
  3179. completion_options.clear();
  3180. completion_index=0;
  3181. completion_base=s;
  3182. Vector<float> sim_cache;
  3183. for(int i=0;i<completion_strings.size();i++) {
  3184. if (s == completion_strings[i]) {
  3185. // A perfect match, stop completion
  3186. _cancel_completion();
  3187. return;
  3188. }
  3189. if (s.is_subsequence_ofi(completion_strings[i])) {
  3190. // don't remove duplicates if no input is provided
  3191. if (s != "" && completion_options.find(completion_strings[i]) != -1) {
  3192. continue;
  3193. }
  3194. // Calculate the similarity to keep completions in good order
  3195. float similarity;
  3196. if (completion_strings[i].to_lower().begins_with(s.to_lower())) {
  3197. // Substrings are the best candidates
  3198. similarity = 1.1;
  3199. } else {
  3200. // Otherwise compute the similarity
  3201. similarity = s.to_lower().similarity(completion_strings[i].to_lower());
  3202. }
  3203. int comp_size = completion_options.size();
  3204. if (comp_size == 0) {
  3205. completion_options.push_back(completion_strings[i]);
  3206. sim_cache.push_back(similarity);
  3207. } else {
  3208. float comp_sim;
  3209. int pos = 0;
  3210. do {
  3211. comp_sim = sim_cache[pos++];
  3212. } while(pos < comp_size && similarity < comp_sim);
  3213. pos = similarity > comp_sim ? pos - 1 : pos; // Pos will be off by one
  3214. completion_options.insert(pos, completion_strings[i]);
  3215. sim_cache.insert(pos, similarity);
  3216. }
  3217. }
  3218. }
  3219. if (completion_options.size()==0) {
  3220. //no options to complete, cancel
  3221. _cancel_completion();
  3222. return;
  3223. }
  3224. // The top of the list is the best match
  3225. completion_current=completion_options[0];
  3226. #if 0 // even there's only one option, user still get the chance to choose using it or not
  3227. if (completion_options.size()==1) {
  3228. //one option to complete, just complete it automagically
  3229. _confirm_completion();
  3230. // insert_text_at_cursor(completion_options[0].substr(s.length(),completion_options[0].length()-s.length()));
  3231. _cancel_completion();
  3232. return;
  3233. }
  3234. #endif
  3235. completion_enabled=true;
  3236. }
  3237. void TextEdit::query_code_comple() {
  3238. String l = text[cursor.line];
  3239. int ofs = CLAMP(cursor.column,0,l.length());
  3240. bool inquote=false;
  3241. int c=ofs-1;
  3242. while(c>=0) {
  3243. if (l[c]=='"' || l[c]=='\'')
  3244. inquote=!inquote;
  3245. c--;
  3246. }
  3247. if (ofs>0 && (inquote || _is_completable(l[ofs-1]) || completion_prefixes.has(String::chr(l[ofs-1]))))
  3248. emit_signal("request_completion");
  3249. }
  3250. void TextEdit::set_code_hint(const String& p_hint) {
  3251. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
  3252. raised_from_completion = true;
  3253. completion_hint=p_hint;
  3254. completion_hint_offset=-0xFFFF;
  3255. update();
  3256. }
  3257. void TextEdit::code_complete(const Vector<String> &p_strings) {
  3258. VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
  3259. raised_from_completion = true;
  3260. completion_strings=p_strings;
  3261. completion_active=true;
  3262. completion_current="";
  3263. completion_index=0;
  3264. _update_completion_candidates();
  3265. //
  3266. }
  3267. String TextEdit::get_word_at_pos(const Vector2& p_pos) const {
  3268. int row,col;
  3269. _get_mouse_pos(p_pos, row, col);
  3270. String s = text[row];
  3271. if (s.length()==0)
  3272. return "";
  3273. int beg=CLAMP(col,0,s.length());
  3274. int end=beg;
  3275. if (s[beg]>32 || beg==s.length()) {
  3276. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  3277. while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) {
  3278. beg--;
  3279. }
  3280. while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) {
  3281. end++;
  3282. }
  3283. if (end<s.length())
  3284. end+=1;
  3285. return s.substr(beg,end-beg);
  3286. }
  3287. return String();
  3288. }
  3289. String TextEdit::get_tooltip(const Point2& p_pos) const {
  3290. if (!tooltip_obj)
  3291. return Control::get_tooltip(p_pos);
  3292. int row,col;
  3293. _get_mouse_pos(p_pos, row, col);
  3294. String s = text[row];
  3295. if (s.length()==0)
  3296. return Control::get_tooltip(p_pos);
  3297. int beg=CLAMP(col,0,s.length());
  3298. int end=beg;
  3299. if (s[beg]>32 || beg==s.length()) {
  3300. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  3301. while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) {
  3302. beg--;
  3303. }
  3304. while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) {
  3305. end++;
  3306. }
  3307. if (end<s.length())
  3308. end+=1;
  3309. String tt = tooltip_obj->call(tooltip_func,s.substr(beg,end-beg),tooltip_ud);
  3310. return tt;
  3311. }
  3312. return Control::get_tooltip(p_pos);
  3313. }
  3314. void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName& p_function,const Variant& p_udata) {
  3315. tooltip_obj=p_obj;
  3316. tooltip_func=p_function;
  3317. tooltip_ud=p_udata;
  3318. }
  3319. void TextEdit::set_line(int line, String new_text)
  3320. {
  3321. if (line < 0 || line > text.size())
  3322. return;
  3323. _remove_text(line, 0, line, text[line].length());
  3324. _insert_text(line, 0, new_text);
  3325. if (cursor.line==line) {
  3326. cursor.column=MIN(cursor.column,new_text.length());
  3327. }
  3328. }
  3329. void TextEdit::insert_at(const String &p_text, int at)
  3330. {
  3331. cursor_set_column(0);
  3332. cursor_set_line(at);
  3333. _insert_text(at, 0, p_text+"\n");
  3334. }
  3335. void TextEdit::set_show_line_numbers(bool p_show) {
  3336. line_numbers=p_show;
  3337. update();
  3338. }
  3339. bool TextEdit::is_show_line_numbers_enabled() const {
  3340. return line_numbers;
  3341. }
  3342. void TextEdit::set_draw_breakpoint_gutter(bool p_draw) {
  3343. draw_breakpoint_gutter = p_draw;
  3344. update();
  3345. }
  3346. bool TextEdit::is_drawing_breakpoint_gutter() const {
  3347. return draw_breakpoint_gutter;
  3348. }
  3349. void TextEdit::set_breakpoint_gutter_width(int p_gutter_width) {
  3350. breakpoint_gutter_width = p_gutter_width;
  3351. update();
  3352. }
  3353. int TextEdit::get_breakpoint_gutter_width() const {
  3354. return cache.breakpoint_gutter_width;
  3355. }
  3356. bool TextEdit::is_text_field() const {
  3357. return true;
  3358. }
  3359. void TextEdit::menu_option(int p_option) {
  3360. switch( p_option ) {
  3361. case MENU_CUT: {
  3362. if (!readonly) {
  3363. cut();
  3364. }
  3365. } break;
  3366. case MENU_COPY: {
  3367. copy();
  3368. } break;
  3369. case MENU_PASTE: {
  3370. if (!readonly) {
  3371. paste();
  3372. }
  3373. } break;
  3374. case MENU_CLEAR: {
  3375. if (!readonly) {
  3376. clear();
  3377. }
  3378. } break;
  3379. case MENU_SELECT_ALL: {
  3380. select_all();
  3381. } break;
  3382. case MENU_UNDO: {
  3383. undo();
  3384. } break;
  3385. };
  3386. }
  3387. void TextEdit::set_select_identifiers_on_hover(bool p_enable) {
  3388. select_identifiers_enabled=p_enable;
  3389. }
  3390. bool TextEdit::is_selecting_identifiers_on_hover_enabled() const {
  3391. return select_identifiers_enabled;
  3392. }
  3393. void TextEdit::set_context_menu_enabled(bool p_enable) {
  3394. context_menu_enabled = p_enable;
  3395. }
  3396. PopupMenu *TextEdit::get_menu() const {
  3397. return menu;
  3398. }
  3399. void TextEdit::_bind_methods() {
  3400. ObjectTypeDB::bind_method(_MD("_input_event"),&TextEdit::_input_event);
  3401. ObjectTypeDB::bind_method(_MD("_scroll_moved"),&TextEdit::_scroll_moved);
  3402. ObjectTypeDB::bind_method(_MD("_cursor_changed_emit"),&TextEdit::_cursor_changed_emit);
  3403. ObjectTypeDB::bind_method(_MD("_text_changed_emit"),&TextEdit::_text_changed_emit);
  3404. ObjectTypeDB::bind_method(_MD("_push_current_op"),&TextEdit::_push_current_op);
  3405. ObjectTypeDB::bind_method(_MD("_click_selection_held"),&TextEdit::_click_selection_held);
  3406. ObjectTypeDB::bind_method(_MD("_toggle_draw_caret"),&TextEdit::_toggle_draw_caret);
  3407. BIND_CONSTANT( SEARCH_MATCH_CASE );
  3408. BIND_CONSTANT( SEARCH_WHOLE_WORDS );
  3409. BIND_CONSTANT( SEARCH_BACKWARDS );
  3410. /*
  3411. ObjectTypeDB::bind_method(_MD("delete_char"),&TextEdit::delete_char);
  3412. ObjectTypeDB::bind_method(_MD("delete_line"),&TextEdit::delete_line);
  3413. */
  3414. ObjectTypeDB::bind_method(_MD("set_text","text"),&TextEdit::set_text);
  3415. ObjectTypeDB::bind_method(_MD("insert_text_at_cursor","text"),&TextEdit::insert_text_at_cursor);
  3416. ObjectTypeDB::bind_method(_MD("get_line_count"),&TextEdit::get_line_count);
  3417. ObjectTypeDB::bind_method(_MD("get_text"),&TextEdit::get_text);
  3418. ObjectTypeDB::bind_method(_MD("get_line","line"),&TextEdit::get_line);
  3419. ObjectTypeDB::bind_method(_MD("cursor_set_column","column","adjust_viewport"),&TextEdit::cursor_set_column,DEFVAL(false));
  3420. ObjectTypeDB::bind_method(_MD("cursor_set_line","line","adjust_viewport"),&TextEdit::cursor_set_line,DEFVAL(false));
  3421. ObjectTypeDB::bind_method(_MD("cursor_get_column"),&TextEdit::cursor_get_column);
  3422. ObjectTypeDB::bind_method(_MD("cursor_get_line"),&TextEdit::cursor_get_line);
  3423. ObjectTypeDB::bind_method(_MD("cursor_set_blink_enabled", "enable"),&TextEdit::cursor_set_blink_enabled);
  3424. ObjectTypeDB::bind_method(_MD("cursor_get_blink_enabled"),&TextEdit::cursor_get_blink_enabled);
  3425. ObjectTypeDB::bind_method(_MD("cursor_set_blink_speed", "blink_speed"),&TextEdit::cursor_set_blink_speed);
  3426. ObjectTypeDB::bind_method(_MD("cursor_get_blink_speed"),&TextEdit::cursor_get_blink_speed);
  3427. ObjectTypeDB::bind_method(_MD("cursor_set_block_mode", "enable"), &TextEdit::cursor_set_block_mode);
  3428. ObjectTypeDB::bind_method(_MD("cursor_is_block_mode"), &TextEdit::cursor_is_block_mode);
  3429. ObjectTypeDB::bind_method(_MD("set_readonly","enable"),&TextEdit::set_readonly);
  3430. ObjectTypeDB::bind_method(_MD("set_wrap","enable"),&TextEdit::set_wrap);
  3431. ObjectTypeDB::bind_method(_MD("set_max_chars","amount"),&TextEdit::set_max_chars);
  3432. ObjectTypeDB::bind_method(_MD("cut"),&TextEdit::cut);
  3433. ObjectTypeDB::bind_method(_MD("copy"),&TextEdit::copy);
  3434. ObjectTypeDB::bind_method(_MD("paste"),&TextEdit::paste);
  3435. ObjectTypeDB::bind_method(_MD("select_all"),&TextEdit::select_all);
  3436. ObjectTypeDB::bind_method(_MD("select","from_line","from_column","to_line","to_column"),&TextEdit::select);
  3437. ObjectTypeDB::bind_method(_MD("is_selection_active"),&TextEdit::is_selection_active);
  3438. ObjectTypeDB::bind_method(_MD("get_selection_from_line"),&TextEdit::get_selection_from_line);
  3439. ObjectTypeDB::bind_method(_MD("get_selection_from_column"),&TextEdit::get_selection_from_column);
  3440. ObjectTypeDB::bind_method(_MD("get_selection_to_line"),&TextEdit::get_selection_to_line);
  3441. ObjectTypeDB::bind_method(_MD("get_selection_to_column"),&TextEdit::get_selection_to_column);
  3442. ObjectTypeDB::bind_method(_MD("get_selection_text"),&TextEdit::get_selection_text);
  3443. ObjectTypeDB::bind_method(_MD("get_word_under_cursor"),&TextEdit::get_word_under_cursor);
  3444. ObjectTypeDB::bind_method(_MD("search","flags","from_line","from_column","to_line","to_column"),&TextEdit::_search_bind);
  3445. ObjectTypeDB::bind_method(_MD("undo"),&TextEdit::undo);
  3446. ObjectTypeDB::bind_method(_MD("redo"),&TextEdit::redo);
  3447. ObjectTypeDB::bind_method(_MD("clear_undo_history"),&TextEdit::clear_undo_history);
  3448. ObjectTypeDB::bind_method(_MD("set_show_line_numbers", "enable"), &TextEdit::set_show_line_numbers);
  3449. ObjectTypeDB::bind_method(_MD("is_show_line_numbers_enabled"), &TextEdit::is_show_line_numbers_enabled);
  3450. ObjectTypeDB::bind_method(_MD("set_highlight_all_occurrences", "enable"), &TextEdit::set_highlight_all_occurrences);
  3451. ObjectTypeDB::bind_method(_MD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  3452. ObjectTypeDB::bind_method(_MD("set_syntax_coloring","enable"),&TextEdit::set_syntax_coloring);
  3453. ObjectTypeDB::bind_method(_MD("is_syntax_coloring_enabled"),&TextEdit::is_syntax_coloring_enabled);
  3454. ObjectTypeDB::bind_method(_MD("add_keyword_color","keyword","color"),&TextEdit::add_keyword_color);
  3455. ObjectTypeDB::bind_method(_MD("add_color_region","begin_key","end_key","color","line_only"),&TextEdit::add_color_region,DEFVAL(false));
  3456. ObjectTypeDB::bind_method(_MD("set_symbol_color","color"),&TextEdit::set_symbol_color);
  3457. ObjectTypeDB::bind_method(_MD("set_custom_bg_color","color"),&TextEdit::set_custom_bg_color);
  3458. ObjectTypeDB::bind_method(_MD("clear_colors"),&TextEdit::clear_colors);
  3459. ObjectTypeDB::bind_method(_MD("menu_option"),&TextEdit::menu_option);
  3460. ObjectTypeDB::bind_method(_MD("get_menu:PopupMenu"),&TextEdit::get_menu);
  3461. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), _SCS("set_syntax_coloring"), _SCS("is_syntax_coloring_enabled"));
  3462. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), _SCS("set_show_line_numbers"), _SCS("is_show_line_numbers_enabled"));
  3463. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), _SCS("set_highlight_all_occurrences"), _SCS("is_highlight_all_occurrences_enabled"));
  3464. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret/block_caret"), _SCS("cursor_set_block_mode"), _SCS("cursor_is_block_mode"));
  3465. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret/caret_blink"), _SCS("cursor_set_blink_enabled"), _SCS("cursor_get_blink_enabled"));
  3466. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret/caret_blink_speed",PROPERTY_HINT_RANGE,"0.1,10,0.1"), _SCS("cursor_set_blink_speed"),_SCS("cursor_get_blink_speed") );
  3467. ADD_SIGNAL(MethodInfo("cursor_changed"));
  3468. ADD_SIGNAL(MethodInfo("text_changed"));
  3469. ADD_SIGNAL(MethodInfo("request_completion"));
  3470. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo( Variant::INT, "row")));
  3471. ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING,"symbol"),PropertyInfo( Variant::INT, "row"),PropertyInfo( Variant::INT, "column")));
  3472. BIND_CONSTANT( MENU_CUT );
  3473. BIND_CONSTANT( MENU_COPY );
  3474. BIND_CONSTANT( MENU_PASTE );
  3475. BIND_CONSTANT( MENU_CLEAR );
  3476. BIND_CONSTANT( MENU_SELECT_ALL );
  3477. BIND_CONSTANT( MENU_UNDO );
  3478. BIND_CONSTANT( MENU_MAX );
  3479. }
  3480. TextEdit::TextEdit() {
  3481. readonly=false;
  3482. setting_row=false;
  3483. draw_tabs=false;
  3484. draw_caret=true;
  3485. max_chars=0;
  3486. clear();
  3487. wrap=false;
  3488. set_focus_mode(FOCUS_ALL);
  3489. _update_caches();
  3490. cache.size=Size2(1,1);
  3491. cache.row_height=1;
  3492. cache.line_spacing=1;
  3493. cache.line_number_w=1;
  3494. cache.breakpoint_gutter_width=0;
  3495. breakpoint_gutter_width = 0;
  3496. tab_size=4;
  3497. text.set_tab_size(tab_size);
  3498. text.clear();
  3499. // text.insert(1,"Mongolia..");
  3500. // text.insert(2,"PAIS GENEROSO!!");
  3501. text.set_color_regions(&color_regions);
  3502. h_scroll = memnew( HScrollBar );
  3503. v_scroll = memnew( VScrollBar );
  3504. add_child(h_scroll);
  3505. add_child(v_scroll);
  3506. updating_scrolls=false;
  3507. selection.active=false;
  3508. h_scroll->connect("value_changed", this,"_scroll_moved");
  3509. v_scroll->connect("value_changed", this,"_scroll_moved");
  3510. cursor_changed_dirty=false;
  3511. text_changed_dirty=false;
  3512. selection.selecting_mode=Selection::MODE_NONE;
  3513. selection.selecting_line=0;
  3514. selection.selecting_column=0;
  3515. selection.selecting_text=false;
  3516. selection.active=false;
  3517. syntax_coloring=false;
  3518. block_caret=false;
  3519. caret_blink_enabled=false;
  3520. caret_blink_timer = memnew(Timer);
  3521. add_child(caret_blink_timer);
  3522. caret_blink_timer->set_wait_time(0.65);
  3523. caret_blink_timer->connect("timeout", this,"_toggle_draw_caret");
  3524. cursor_set_blink_enabled(false);
  3525. custom_bg_color=Color(0,0,0,0);
  3526. idle_detect = memnew( Timer );
  3527. add_child(idle_detect);
  3528. idle_detect->set_one_shot(true);
  3529. idle_detect->set_wait_time(GLOBAL_DEF("display/text_edit_idle_detect_sec",3));
  3530. idle_detect->connect("timeout", this,"_push_current_op");
  3531. click_select_held = memnew( Timer );
  3532. add_child(click_select_held);
  3533. click_select_held->set_wait_time(0.05);
  3534. click_select_held->connect("timeout", this,"_click_selection_held");
  3535. #if 0
  3536. syntax_coloring=true;
  3537. keywords["void"]=Color(0.3,0.0,0.1);
  3538. keywords["int"]=Color(0.3,0.0,0.1);
  3539. keywords["function"]=Color(0.3,0.0,0.1);
  3540. keywords["class"]=Color(0.3,0.0,0.1);
  3541. keywords["extends"]=Color(0.3,0.0,0.1);
  3542. keywords["constructor"]=Color(0.3,0.0,0.1);
  3543. symbol_color=Color(0.1,0.0,0.3,1.0);
  3544. color_regions.push_back(ColorRegion("/*","*/",Color(0.4,0.6,0,4)));
  3545. color_regions.push_back(ColorRegion("//","",Color(0.6,0.6,0.4)));
  3546. color_regions.push_back(ColorRegion("\"","\"",Color(0.4,0.7,0.7)));
  3547. color_regions.push_back(ColorRegion("'","'",Color(0.4,0.8,0.8)));
  3548. color_regions.push_back(ColorRegion("#","",Color(0.2,1.0,0.2)));
  3549. #endif
  3550. current_op.type=TextOperation::TYPE_NONE;
  3551. undo_enabled=true;
  3552. undo_stack_pos=NULL;
  3553. setting_text=false;
  3554. last_dblclk=0;
  3555. current_op.version=0;
  3556. version=0;
  3557. saved_version=0;
  3558. completion_enabled=false;
  3559. completion_active=false;
  3560. completion_line_ofs=0;
  3561. tooltip_obj=NULL;
  3562. line_numbers=false;
  3563. draw_breakpoint_gutter=false;
  3564. next_operation_is_complex=false;
  3565. scroll_past_end_of_file_enabled=false;
  3566. auto_brace_completion_enabled=false;
  3567. brace_matching_enabled=false;
  3568. highlight_all_occurrences=false;
  3569. auto_indent=false;
  3570. insert_mode = false;
  3571. window_has_focus=true;
  3572. select_identifiers_enabled=false;
  3573. raised_from_completion = false;
  3574. context_menu_enabled=true;
  3575. menu = memnew( PopupMenu );
  3576. add_child(menu);
  3577. menu->add_item(TTR("Cut"),MENU_CUT,KEY_MASK_CMD|KEY_X);
  3578. menu->add_item(TTR("Copy"),MENU_COPY,KEY_MASK_CMD|KEY_C);
  3579. menu->add_item(TTR("Paste"),MENU_PASTE,KEY_MASK_CMD|KEY_V);
  3580. menu->add_separator();
  3581. menu->add_item(TTR("Select All"),MENU_SELECT_ALL,KEY_MASK_CMD|KEY_A);
  3582. menu->add_item(TTR("Clear"),MENU_CLEAR);
  3583. menu->add_separator();
  3584. menu->add_item(TTR("Undo"),MENU_UNDO,KEY_MASK_CMD|KEY_Z);
  3585. menu->connect("item_pressed",this,"menu_option");
  3586. }
  3587. TextEdit::~TextEdit()
  3588. {
  3589. }