text_edit.cpp 108 KB

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