text_edit.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738
  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-2015 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/os.h"
  32. #include "globals.h"
  33. #include "message_queue.h"
  34. #define TAB_PIXELS
  35. static bool _is_text_char(CharType c) {
  36. return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_';
  37. }
  38. static bool _is_symbol(CharType c) {
  39. return c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t');
  40. }
  41. static bool _is_pair_right_symbol(CharType c) {
  42. return
  43. c == '"' ||
  44. c == '\'' ||
  45. c == ')' ||
  46. c == ']' ||
  47. c == '}';
  48. }
  49. static bool _is_pair_left_symbol(CharType c) {
  50. return
  51. c == '"' ||
  52. c == '\'' ||
  53. c == '(' ||
  54. c == '[' ||
  55. c == '{';
  56. }
  57. static bool _is_pair_symbol(CharType c) {
  58. return _is_pair_left_symbol(c) || _is_pair_right_symbol(c);
  59. }
  60. static CharType _get_right_pair_symbol(CharType c) {
  61. if(c == '"')
  62. return '"';
  63. if(c == '\'')
  64. return '\'';
  65. if(c == '(')
  66. return ')';
  67. if(c == '[')
  68. return ']';
  69. if(c == '{')
  70. return '}';
  71. return 0;
  72. }
  73. void TextEdit::Text::set_font(const Ref<Font>& p_font) {
  74. font=p_font;
  75. }
  76. void TextEdit::Text::set_tab_size(int p_tab_size) {
  77. tab_size=p_tab_size;
  78. }
  79. void TextEdit::Text::_update_line_cache(int p_line) const {
  80. int w =0;
  81. int tab_w=font->get_char_size(' ').width;
  82. int len = text[p_line].data.length();
  83. const CharType *str = text[p_line].data.c_str();
  84. //update width
  85. for(int i=0;i<len;i++) {
  86. if (str[i]=='\t') {
  87. int left = w%tab_w;
  88. if (left==0)
  89. w+=tab_w;
  90. else
  91. w+=tab_w-w%tab_w; // is right...
  92. } else {
  93. w+=font->get_char_size(str[i],str[i+1]).width;
  94. }
  95. }
  96. text[p_line].width_cache=w;
  97. //update regions
  98. text[p_line].region_info.clear();
  99. for(int i=0;i<len;i++) {
  100. if (!_is_symbol(str[i]))
  101. continue;
  102. if (str[i]=='\\') {
  103. i++; //skip quoted anything
  104. continue;
  105. }
  106. int left=len-i;
  107. for(int j=0;j<color_regions->size();j++) {
  108. const ColorRegion& cr=color_regions->operator [](j);
  109. /* BEGIN */
  110. int lr=cr.begin_key.length();
  111. if (lr==0 || lr>left)
  112. continue;
  113. const CharType* kc = cr.begin_key.c_str();
  114. bool match=true;
  115. for(int k=0;k<lr;k++) {
  116. if (kc[k]!=str[i+k]) {
  117. match=false;
  118. break;
  119. }
  120. }
  121. if (match) {
  122. ColorRegionInfo cri;
  123. cri.end=false;
  124. cri.region=j;
  125. text[p_line].region_info[i]=cri;
  126. i+=lr-1;
  127. break;
  128. }
  129. /* END */
  130. lr=cr.end_key.length();
  131. if (lr==0 || lr>left)
  132. continue;
  133. kc = cr.end_key.c_str();
  134. match=true;
  135. for(int k=0;k<lr;k++) {
  136. if (kc[k]!=str[i+k]) {
  137. match=false;
  138. break;
  139. }
  140. }
  141. if (match) {
  142. ColorRegionInfo cri;
  143. cri.end=true;
  144. cri.region=j;
  145. text[p_line].region_info[i]=cri;
  146. i+=lr-1;
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. const Map<int,TextEdit::Text::ColorRegionInfo>& TextEdit::Text::get_color_region_info(int p_line) {
  153. Map<int,ColorRegionInfo> *cri=NULL;
  154. ERR_FAIL_INDEX_V(p_line,text.size(),*cri); //enjoy your crash
  155. if (text[p_line].width_cache==-1) {
  156. _update_line_cache(p_line);
  157. }
  158. return text[p_line].region_info;
  159. }
  160. int TextEdit::Text::get_line_width(int p_line) const {
  161. ERR_FAIL_INDEX_V(p_line,text.size(),-1);
  162. if (text[p_line].width_cache==-1) {
  163. _update_line_cache(p_line);
  164. }
  165. return text[p_line].width_cache;
  166. }
  167. void TextEdit::Text::clear_caches() {
  168. for(int i=0;i<text.size();i++)
  169. text[i].width_cache=-1;
  170. }
  171. void TextEdit::Text::clear() {
  172. text.clear();;
  173. insert(0,"");
  174. }
  175. int TextEdit::Text::get_max_width() const {
  176. //quite some work.. but should be fast enough.
  177. int max = 0;
  178. for(int i=0;i<text.size();i++)
  179. max=MAX(max,get_line_width(i));
  180. return max;
  181. }
  182. void TextEdit::Text::set(int p_line,const String& p_text) {
  183. ERR_FAIL_INDEX(p_line,text.size());
  184. text[p_line].width_cache=-1;
  185. text[p_line].data=p_text;
  186. }
  187. void TextEdit::Text::insert(int p_at,const String& p_text) {
  188. Line line;
  189. line.marked=false;
  190. line.breakpoint=false;
  191. line.width_cache=-1;
  192. line.data=p_text;
  193. text.insert(p_at,line);
  194. }
  195. void TextEdit::Text::remove(int p_at) {
  196. text.remove(p_at);
  197. }
  198. void TextEdit::_update_scrollbars() {
  199. Size2 size = get_size();
  200. Size2 hmin = h_scroll->get_combined_minimum_size();
  201. Size2 vmin = v_scroll->get_combined_minimum_size();
  202. v_scroll->set_begin( Point2(size.width - vmin.width, cache.style_normal->get_margin(MARGIN_TOP)) );
  203. v_scroll->set_end( Point2(size.width, size.height - cache.style_normal->get_margin(MARGIN_TOP) - cache.style_normal->get_margin(MARGIN_BOTTOM)) );
  204. h_scroll->set_begin( Point2( 0, size.height - hmin.height) );
  205. h_scroll->set_end( Point2(size.width-vmin.width, size.height) );
  206. int hscroll_rows = ((hmin.height-1)/get_row_height())+1;
  207. int visible_rows = get_visible_rows();
  208. int total_rows = text.size();
  209. int vscroll_pixels = v_scroll->get_combined_minimum_size().width;
  210. int visible_width = size.width - cache.style_normal->get_minimum_size().width;
  211. int total_width = text.get_max_width();
  212. bool use_hscroll=true;
  213. bool use_vscroll=true;
  214. if (total_rows <= visible_rows && total_width <= visible_width) {
  215. //thanks yessopie for this clever bit of logic
  216. use_hscroll=false;
  217. use_vscroll=false;
  218. } else {
  219. if (total_rows > visible_rows && total_width <= visible_width - vscroll_pixels) {
  220. //thanks yessopie for this clever bit of logic
  221. use_hscroll=false;
  222. }
  223. if (total_rows <= visible_rows - hscroll_rows && total_width > visible_width) {
  224. //thanks yessopie for this clever bit of logic
  225. use_vscroll=false;
  226. }
  227. }
  228. updating_scrolls=true;
  229. if (use_vscroll) {
  230. v_scroll->show();
  231. v_scroll->set_max(total_rows);
  232. v_scroll->set_page(visible_rows);
  233. v_scroll->set_val(cursor.line_ofs);
  234. } else {
  235. cursor.line_ofs = 0;
  236. v_scroll->hide();
  237. }
  238. if (use_hscroll) {
  239. h_scroll->show();
  240. h_scroll->set_max(total_width);
  241. h_scroll->set_page(visible_width);
  242. h_scroll->set_val(cursor.x_ofs);
  243. } else {
  244. h_scroll->hide();
  245. }
  246. updating_scrolls=false;
  247. }
  248. void TextEdit::_notification(int p_what) {
  249. switch(p_what) {
  250. case NOTIFICATION_ENTER_TREE: {
  251. _update_caches();
  252. if (cursor_changed_dirty)
  253. MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit");
  254. if (text_changed_dirty)
  255. MessageQueue::get_singleton()->push_call(this,"_text_changed_emit");
  256. } break;
  257. case NOTIFICATION_RESIZED: {
  258. cache.size=get_size();
  259. adjust_viewport_to_cursor();
  260. } break;
  261. case NOTIFICATION_THEME_CHANGED: {
  262. _update_caches();
  263. };
  264. case NOTIFICATION_DRAW: {
  265. int line_number_char_count=0;
  266. {
  267. int lc=text.size()+1;
  268. cache.line_number_w=0;
  269. while(lc) {
  270. cache.line_number_w+=1;
  271. lc/=10;
  272. };
  273. if (line_numbers) {
  274. line_number_char_count=cache.line_number_w;
  275. cache.line_number_w=(cache.line_number_w+1)*cache.font->get_char_size('0').width;
  276. } else {
  277. cache.line_number_w=0;
  278. }
  279. }
  280. _update_scrollbars();
  281. RID ci = get_canvas_item();
  282. int xmargin_beg=cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w;
  283. int xmargin_end=cache.size.width-cache.style_normal->get_margin(MARGIN_RIGHT);
  284. //let's do it easy for now:
  285. cache.style_normal->draw(ci,Rect2(Point2(),cache.size));
  286. if (has_focus())
  287. cache.style_focus->draw(ci,Rect2(Point2(),cache.size));
  288. int ascent=cache.font->get_ascent();
  289. int visible_rows = get_visible_rows();
  290. int tab_w = cache.font->get_char_size(' ').width*tab_size;
  291. Color color = cache.font_color;
  292. int in_region=-1;
  293. if (syntax_coloring) {
  294. if (custom_bg_color.a>0.01) {
  295. Point2i ofs = Point2i(cache.style_normal->get_offset())/2.0;
  296. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(ofs, get_size()-cache.style_normal->get_minimum_size()+ofs),custom_bg_color);
  297. }
  298. //compute actual region to start (may be inside say, a comment).
  299. //slow in very large documments :( but ok for source!
  300. for(int i=0;i<cursor.line_ofs;i++) {
  301. const Map<int,Text::ColorRegionInfo>& cri_map=text.get_color_region_info(i);
  302. if (in_region>=0 && color_regions[in_region].line_only) {
  303. in_region=-1; //reset regions that end at end of line
  304. }
  305. for( const Map<int,Text::ColorRegionInfo>::Element* E= cri_map.front();E;E=E->next() ) {
  306. const Text::ColorRegionInfo &cri=E->get();
  307. if (in_region==-1) {
  308. if (!cri.end) {
  309. in_region=cri.region;
  310. }
  311. } else if (in_region==cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
  312. if (cri.end || color_regions[cri.region].eq) {
  313. in_region=-1;
  314. }
  315. }
  316. }
  317. }
  318. }
  319. int brace_open_match_line=-1;
  320. int brace_open_match_column=-1;
  321. bool brace_open_matching=false;
  322. bool brace_open_mismatch=false;
  323. int brace_close_match_line=-1;
  324. int brace_close_match_column=-1;
  325. bool brace_close_matching=false;
  326. bool brace_close_mismatch=false;
  327. if (brace_matching_enabled) {
  328. if (cursor.column<text[cursor.line].length()) {
  329. //check for open
  330. CharType c = text[cursor.line][cursor.column];
  331. CharType closec=0;
  332. if (c=='[') {
  333. closec=']';
  334. } else if (c=='{') {
  335. closec='}';
  336. } else if (c=='(') {
  337. closec=')';
  338. }
  339. if (closec!=0) {
  340. int stack=1;
  341. for(int i=cursor.line;i<text.size();i++) {
  342. int from = i==cursor.line?cursor.column+1:0;
  343. for(int j=from;j<text[i].length();j++) {
  344. CharType cc = text[i][j];
  345. //ignore any brackets inside a string
  346. if (cc== '"' || cc == '\'') {
  347. CharType quotation = cc;
  348. do {
  349. j++;
  350. if (!(j<text[i].length())) {
  351. break;
  352. }
  353. cc=text[i][j];
  354. //skip over escaped quotation marks inside strings
  355. if (cc=='\\') {
  356. bool escaped = true;
  357. while (j+1<text[i].length() && text[i][j+1]=='\\') {
  358. escaped=!escaped;
  359. j++;
  360. }
  361. if (escaped) {
  362. j++;
  363. continue;
  364. }
  365. }
  366. } while (cc!= quotation);
  367. } else if (cc==c)
  368. stack++;
  369. else if (cc==closec)
  370. stack--;
  371. if (stack==0) {
  372. brace_open_match_line=i;
  373. brace_open_match_column=j;
  374. brace_open_matching=true;
  375. break;
  376. }
  377. }
  378. if (brace_open_match_line!=-1)
  379. break;
  380. }
  381. if (!brace_open_matching)
  382. brace_open_mismatch=true;
  383. }
  384. }
  385. if (cursor.column>0) {
  386. CharType c = text[cursor.line][cursor.column-1];
  387. CharType closec=0;
  388. if (c==']') {
  389. closec='[';
  390. } else if (c=='}') {
  391. closec='{';
  392. } else if (c==')') {
  393. closec='(';
  394. }
  395. if (closec!=0) {
  396. int stack=1;
  397. for(int i=cursor.line;i>=0;i--) {
  398. int from = i==cursor.line?cursor.column-2:text[i].length()-1;
  399. for(int j=from;j>=0;j--) {
  400. CharType cc = text[i][j];
  401. //ignore any brackets inside a string
  402. if (cc== '"' || cc == '\'') {
  403. CharType quotation = cc;
  404. do {
  405. j--;
  406. if (!(j>=0)) {
  407. break;
  408. }
  409. cc=text[i][j];
  410. //skip over escaped quotation marks inside strings
  411. if (cc==quotation) {
  412. bool escaped = false;
  413. while (j-1>=0 && text[i][j-1]=='\\') {
  414. escaped=!escaped;
  415. j--;
  416. }
  417. if (escaped) {
  418. j--;
  419. cc='\\';
  420. continue;
  421. }
  422. }
  423. } while (cc!= quotation);
  424. } else if (cc==c)
  425. stack++;
  426. else if (cc==closec)
  427. stack--;
  428. if (stack==0) {
  429. brace_close_match_line=i;
  430. brace_close_match_column=j;
  431. brace_close_matching=true;
  432. break;
  433. }
  434. }
  435. if (brace_close_match_line!=-1)
  436. break;
  437. }
  438. if (!brace_close_matching)
  439. brace_close_mismatch=true;
  440. }
  441. }
  442. }
  443. int deregion=0; //force it to clear inrgion
  444. Point2 cursor_pos;
  445. for (int i=0;i<visible_rows;i++) {
  446. int line=i+cursor.line_ofs;
  447. if (line<0 || line>=(int)text.size())
  448. continue;
  449. const String &str=text[line];
  450. int char_margin=xmargin_beg-cursor.x_ofs;
  451. int char_ofs=0;
  452. int ofs_y=i*get_row_height()+cache.line_spacing/2;
  453. bool prev_is_char=false;
  454. bool in_keyword=false;
  455. Color keyword_color;
  456. if (cache.line_number_w) {
  457. Color fcol = cache.font_color;
  458. fcol.a*=0.4;
  459. String fc = String::num(line+1);
  460. while (fc.length() < line_number_char_count) {
  461. fc="0"+fc;
  462. }
  463. cache.font->draw(ci,Point2(cache.style_normal->get_margin(MARGIN_LEFT),ofs_y+cache.font->get_ascent()),fc,fcol);
  464. }
  465. const Map<int,Text::ColorRegionInfo>& cri_map=text.get_color_region_info(line);
  466. if (text.is_marked(line)) {
  467. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.mark_color);
  468. }
  469. if (text.is_breakpoint(line)) {
  470. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.breakpoint_color);
  471. }
  472. if (line==cursor.line) {
  473. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.current_line_color);
  474. }
  475. for (int j=0;j<str.length();j++) {
  476. //look for keyword
  477. if (deregion>0) {
  478. deregion--;
  479. if (deregion==0)
  480. in_region=-1;
  481. }
  482. if (syntax_coloring && deregion==0) {
  483. color = cache.font_color; //reset
  484. //find keyword
  485. bool is_char = _is_text_char(str[j]);
  486. bool is_symbol=_is_symbol(str[j]);
  487. if (j==0 && in_region>=0 && color_regions[in_region].line_only) {
  488. in_region=-1; //reset regions that end at end of line
  489. }
  490. if (is_symbol && cri_map.has(j)) {
  491. const Text::ColorRegionInfo &cri=cri_map[j];
  492. if (in_region==-1) {
  493. if (!cri.end) {
  494. in_region=cri.region;
  495. }
  496. } else if (in_region==cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
  497. if (cri.end || color_regions[cri.region].eq) {
  498. deregion=color_regions[cri.region].eq?color_regions[cri.region].begin_key.length():color_regions[cri.region].end_key.length();
  499. }
  500. }
  501. }
  502. if (!is_char)
  503. in_keyword=false;
  504. if (in_region==-1 && !in_keyword && is_char && !prev_is_char) {
  505. int to=j;
  506. while(_is_text_char(str[to]) && to<str.length())
  507. to++;
  508. uint32_t hash = String::hash(&str[j],to-j);
  509. StrRange range(&str[j],to-j);
  510. const Color *col=keywords.custom_getptr(range,hash);
  511. if (col) {
  512. in_keyword=true;
  513. keyword_color=*col;
  514. }
  515. }
  516. if (in_region>=0)
  517. color=color_regions[in_region].color;
  518. else if (in_keyword)
  519. color=keyword_color;
  520. else if (is_symbol)
  521. color=symbol_color;
  522. prev_is_char=is_char;
  523. }
  524. int char_w;
  525. //handle tabulator
  526. if (str[j]=='\t') {
  527. int left = char_ofs%tab_w;
  528. if (left==0)
  529. char_w=tab_w;
  530. else
  531. char_w=tab_w-char_ofs%tab_w; // is right...
  532. } else {
  533. char_w=cache.font->get_char_size(str[j],str[j+1]).width;
  534. }
  535. if ( (char_ofs+char_margin)<xmargin_beg) {
  536. char_ofs+=char_w;
  537. continue;
  538. }
  539. if ( (char_ofs+char_margin+char_w)>=xmargin_end) {
  540. if (syntax_coloring)
  541. continue;
  542. else
  543. break;
  544. }
  545. 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));
  546. if (in_selection) {
  547. //inside selection!
  548. 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);
  549. }
  550. if (brace_matching_enabled) {
  551. if ( (brace_open_match_line==line && brace_open_match_column==j) ||
  552. (cursor.column==j && cursor.line==line && (brace_open_matching||brace_open_mismatch))) {
  553. if (brace_open_mismatch)
  554. color=cache.brace_mismatch_color;
  555. cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),'_',str[j+1],in_selection?cache.font_selected_color:color);
  556. }
  557. if (
  558. (brace_close_match_line==line && brace_close_match_column==j) ||
  559. (cursor.column==j+1 && cursor.line==line && (brace_close_matching||brace_close_mismatch))) {
  560. if (brace_close_mismatch)
  561. color=cache.brace_mismatch_color;
  562. cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),'_',str[j+1],in_selection?cache.font_selected_color:color);
  563. }
  564. }
  565. if (str[j]>=32)
  566. 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);
  567. else if (draw_tabs && str[j]=='\t') {
  568. int yofs= (get_row_height() - cache.tab_icon->get_height())/2;
  569. cache.tab_icon->draw(ci, Point2(char_ofs+char_margin,ofs_y+yofs),in_selection?cache.font_selected_color:color);
  570. }
  571. if (cursor.column==j && cursor.line==line) {
  572. cursor_pos = Point2i( char_ofs+char_margin, ofs_y );
  573. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.font_color);
  574. }
  575. char_ofs+=char_w;
  576. }
  577. if (cursor.column==str.length() && cursor.line==line) {
  578. cursor_pos=Point2i( char_ofs+char_margin, ofs_y );
  579. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.font_color);
  580. }
  581. }
  582. if (completion_active) {
  583. // code completion box
  584. Ref<StyleBox> csb = get_stylebox("completion");
  585. Ref<StyleBox> csel = get_stylebox("completion_selected");
  586. int maxlines = get_constant("completion_lines");
  587. int cmax_width = get_constant("completion_max_width")*cache.font->get_char_size('x').x;
  588. Color existing = get_color("completion_existing");
  589. existing.a=0.2;
  590. int scrollw = get_constant("completion_scroll_width");
  591. Color scrollc = get_color("completion_scroll_color");
  592. int lines = MIN(completion_options.size(),maxlines);
  593. int w=0;
  594. int h=lines*get_row_height();
  595. int nofs = cache.font->get_string_size(completion_base).width;
  596. if (completion_options.size() < 50) {
  597. for(int i=0;i<completion_options.size();i++) {
  598. int w2=MIN(cache.font->get_string_size(completion_options[i]).x,cmax_width);
  599. if (w2>w)
  600. w=w2;
  601. }
  602. } else {
  603. w=cmax_width;
  604. }
  605. int th = h + csb->get_minimum_size().y;
  606. if (cursor_pos.y+get_row_height()+th > get_size().height) {
  607. completion_rect.pos.y=cursor_pos.y-th;
  608. } else {
  609. completion_rect.pos.y=cursor_pos.y+get_row_height()+csb->get_offset().y;
  610. }
  611. if (cursor_pos.x-nofs+w+scrollw > get_size().width) {
  612. completion_rect.pos.x=get_size().width-w-scrollw;
  613. } else {
  614. completion_rect.pos.x=cursor_pos.x-nofs;
  615. }
  616. completion_rect.size.width=w+2;
  617. completion_rect.size.height=h;
  618. if (completion_options.size()<=maxlines)
  619. scrollw=0;
  620. draw_style_box(csb,Rect2(completion_rect.pos-csb->get_offset(),completion_rect.size+csb->get_minimum_size()+Size2(scrollw,0)));
  621. int line_from = CLAMP(completion_index - lines/2, 0, completion_options.size() - lines);
  622. draw_style_box(csel,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())));
  623. draw_rect(Rect2(completion_rect.pos,Size2(nofs,completion_rect.size.height)),existing);
  624. for(int i=0;i<lines;i++) {
  625. int l = line_from + i;
  626. ERR_CONTINUE( l < 0 || l>= completion_options.size());
  627. Color text_color = cache.font_color;
  628. for(int j=0;j<color_regions.size();j++) {
  629. if (completion_options[l].begins_with(color_regions[j].begin_key)) {
  630. text_color=color_regions[j].color;
  631. }
  632. }
  633. 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);
  634. }
  635. if (scrollw) {
  636. //draw a small scroll rectangle to show a position in the options
  637. float r = maxlines / (float)completion_options.size();
  638. float o = line_from / (float)completion_options.size();
  639. 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);
  640. }
  641. completion_line_ofs=line_from;
  642. }
  643. if (completion_hint!="") {
  644. Ref<StyleBox> sb = get_stylebox("panel","TooltipPanel");
  645. Ref<Font> font = cache.font;
  646. Color font_color = get_color("font_color","TooltipLabel");
  647. int max_w=0;
  648. int sc = completion_hint.get_slice_count("\n");
  649. int offset=0;
  650. int spacing=0;
  651. for(int i=0;i<sc;i++) {
  652. String l = completion_hint.get_slice("\n",i);
  653. int len = font->get_string_size(l).x;
  654. max_w = MAX(len,max_w);
  655. if (i==0) {
  656. offset = font->get_string_size(l.substr(0,l.find(String::chr(0xFFFF)))).x;
  657. } else {
  658. spacing+=cache.line_spacing;
  659. }
  660. }
  661. Size2 size = Size2(max_w,sc*font->get_height()+spacing);
  662. Size2 minsize = size+sb->get_minimum_size();
  663. if (completion_hint_offset==-0xFFFF) {
  664. completion_hint_offset=cursor_pos.x-offset;
  665. }
  666. Point2 hint_ofs = Vector2(completion_hint_offset,cursor_pos.y-minsize.y);
  667. draw_style_box(sb,Rect2(hint_ofs,minsize));
  668. spacing=0;
  669. for(int i=0;i<sc;i++) {
  670. int begin=0;
  671. int end=0;
  672. String l = completion_hint.get_slice("\n",i);
  673. if (l.find(String::chr(0xFFFF))!=-1) {
  674. begin = font->get_string_size(l.substr(0,l.find(String::chr(0xFFFF)))).x;
  675. end = font->get_string_size(l.substr(0,l.rfind(String::chr(0xFFFF)))).x;
  676. }
  677. 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);
  678. if (end>0) {
  679. Vector2 b = hint_ofs+sb->get_offset()+Vector2(begin,font->get_height()+font->get_height()*i+spacing-1);
  680. draw_line(b,b+Vector2(end-begin,0),font_color);
  681. }
  682. spacing+=cache.line_spacing;
  683. }
  684. }
  685. } break;
  686. case NOTIFICATION_FOCUS_ENTER: {
  687. if (OS::get_singleton()->has_virtual_keyboard())
  688. OS::get_singleton()->show_virtual_keyboard(get_text(),get_global_rect());
  689. } break;
  690. case NOTIFICATION_FOCUS_EXIT: {
  691. if (OS::get_singleton()->has_virtual_keyboard())
  692. OS::get_singleton()->hide_virtual_keyboard();
  693. } break;
  694. }
  695. }
  696. void TextEdit::_consume_pair_symbol(CharType ch) {
  697. int cursor_position_to_move = cursor_get_column() + 1;
  698. CharType ch_single[2] = {ch, 0};
  699. CharType ch_single_pair[2] = {_get_right_pair_symbol(ch), 0};
  700. CharType ch_pair[3] = {ch, _get_right_pair_symbol(ch), 0};
  701. if(is_selection_active()) {
  702. int new_column,new_line;
  703. _begin_compex_operation();
  704. _insert_text(get_selection_from_line(), get_selection_from_column(),
  705. ch_single,
  706. &new_line, &new_column);
  707. int to_col_offset = 0;
  708. if(get_selection_from_line() == get_selection_to_line())
  709. to_col_offset = 1;
  710. _insert_text(get_selection_to_line(),
  711. get_selection_to_column() + to_col_offset,
  712. ch_single_pair,
  713. &new_line,&new_column);
  714. _end_compex_operation();
  715. cursor_set_line(get_selection_to_line());
  716. cursor_set_column(get_selection_to_column() + to_col_offset);
  717. deselect();
  718. update();
  719. return;
  720. }
  721. if( (ch == '\'' || ch == '"') &&
  722. cursor_get_column() > 0 &&
  723. _is_text_char(text[cursor.line][cursor_get_column() - 1])
  724. ) {
  725. insert_text_at_cursor(ch_single);
  726. cursor_set_column(cursor_position_to_move);
  727. return;
  728. }
  729. if(cursor_get_column() < text[cursor.line].length()) {
  730. if(_is_text_char(text[cursor.line][cursor_get_column()])) {
  731. insert_text_at_cursor(ch_single);
  732. cursor_set_column(cursor_position_to_move);
  733. return;
  734. }
  735. if( _is_pair_right_symbol(ch) &&
  736. text[cursor.line][cursor_get_column()] == ch
  737. ) {
  738. cursor_set_column(cursor_position_to_move);
  739. return;
  740. }
  741. }
  742. insert_text_at_cursor(ch_pair);
  743. cursor_set_column(cursor_position_to_move);
  744. return;
  745. }
  746. void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
  747. bool remove_right_symbol = false;
  748. if(cursor.column < text[cursor.line].length() && cursor.column > 0) {
  749. CharType left_char = text[cursor.line][cursor.column - 1];
  750. CharType right_char = text[cursor.line][cursor.column];
  751. if(right_char == _get_right_pair_symbol(left_char)) {
  752. remove_right_symbol = true;
  753. }
  754. }
  755. if(remove_right_symbol) {
  756. _remove_text(prev_line,prev_column,cursor.line,cursor.column + 1);
  757. } else {
  758. _remove_text(prev_line,prev_column,cursor.line,cursor.column);
  759. }
  760. }
  761. void TextEdit::backspace_at_cursor() {
  762. if (readonly)
  763. return;
  764. if (cursor.column==0 && cursor.line==0)
  765. return;
  766. int prev_line = cursor.column?cursor.line:cursor.line-1;
  767. int prev_column = cursor.column?(cursor.column-1):(text[cursor.line-1].length());
  768. if(auto_brace_completion_enabled &&
  769. cursor.column > 0 &&
  770. _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
  771. _consume_backspace_for_pair_symbol(prev_line, prev_column);
  772. } else {
  773. _remove_text(prev_line,prev_column,cursor.line,cursor.column);
  774. }
  775. cursor_set_line(prev_line);
  776. cursor_set_column(prev_column);
  777. }
  778. bool TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
  779. int row=p_mouse.y;
  780. row-=cache.style_normal->get_margin(MARGIN_TOP);
  781. row/=get_row_height();
  782. if (row<0 || row>=get_visible_rows())
  783. return false;
  784. row+=cursor.line_ofs;
  785. int col=0;
  786. if (row>=text.size()) {
  787. row=text.size()-1;
  788. col=text[row].size();
  789. } else {
  790. col=p_mouse.x-(cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w);
  791. col+=cursor.x_ofs;
  792. col=get_char_pos_for( col, get_line(row) );
  793. }
  794. r_row=row;
  795. r_col=col;
  796. return true;
  797. }
  798. void TextEdit::_input_event(const InputEvent& p_input_event) {
  799. switch(p_input_event.type) {
  800. case InputEvent::MOUSE_BUTTON: {
  801. const InputEventMouseButton &mb=p_input_event.mouse_button;
  802. if (completion_active && completion_rect.has_point(Point2(mb.x,mb.y))) {
  803. if (!mb.pressed)
  804. return;
  805. if (mb.button_index==BUTTON_WHEEL_UP) {
  806. if (completion_index>0) {
  807. completion_index--;
  808. completion_current=completion_options[completion_index];
  809. update();
  810. }
  811. }
  812. if (mb.button_index==BUTTON_WHEEL_DOWN) {
  813. if (completion_index<completion_options.size()-1) {
  814. completion_index++;
  815. completion_current=completion_options[completion_index];
  816. update();
  817. }
  818. }
  819. if (mb.button_index==BUTTON_LEFT) {
  820. completion_index=CLAMP(completion_line_ofs+(mb.y-completion_rect.pos.y)/get_row_height(),0,completion_options.size()-1);
  821. completion_current=completion_options[completion_index];
  822. update();
  823. if (mb.doubleclick)
  824. _confirm_completion();
  825. }
  826. return;
  827. } else {
  828. _cancel_completion();
  829. _cancel_code_hint();
  830. }
  831. if (mb.pressed) {
  832. if (mb.button_index==BUTTON_WHEEL_UP) {
  833. v_scroll->set_val( v_scroll->get_val() -3 );
  834. }
  835. if (mb.button_index==BUTTON_WHEEL_DOWN) {
  836. v_scroll->set_val( v_scroll->get_val() +3 );
  837. }
  838. if (mb.button_index==BUTTON_LEFT) {
  839. int row,col;
  840. if (!_get_mouse_pos(Point2i(mb.x,mb.y), row,col))
  841. return;
  842. int prev_col=cursor.column;
  843. int prev_line=cursor.line;
  844. cursor_set_line( row );
  845. cursor_set_column( col );
  846. if (mb.mod.shift && (cursor.column!=prev_col || cursor.line!=prev_line)) {
  847. if (!selection.active) {
  848. selection.active=true;
  849. selection.selecting_mode=Selection::MODE_POINTER;
  850. selection.from_column=prev_col;
  851. selection.from_line=prev_line;
  852. selection.to_column=cursor.column;
  853. selection.to_line=cursor.line;
  854. if (selection.from_line>selection.to_line || (selection.from_line==selection.to_line && selection.from_column>selection.to_column)) {
  855. SWAP(selection.from_column,selection.to_column);
  856. SWAP(selection.from_line,selection.to_line);
  857. selection.shiftclick_left=false;
  858. } else {
  859. selection.shiftclick_left=true;
  860. }
  861. selection.selecting_line=prev_line;
  862. selection.selecting_column=prev_col;
  863. update();
  864. } else {
  865. if (cursor.line<selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column<selection.selecting_column)) {
  866. if (selection.shiftclick_left) {
  867. SWAP(selection.from_column,selection.to_column);
  868. SWAP(selection.from_line,selection.to_line);
  869. selection.shiftclick_left = !selection.shiftclick_left;
  870. }
  871. selection.from_column=cursor.column;
  872. selection.from_line=cursor.line;
  873. } else if (cursor.line>selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column>selection.selecting_column)) {
  874. if (!selection.shiftclick_left) {
  875. SWAP(selection.from_column,selection.to_column);
  876. SWAP(selection.from_line,selection.to_line);
  877. selection.shiftclick_left = !selection.shiftclick_left;
  878. }
  879. selection.to_column=cursor.column;
  880. selection.to_line=cursor.line;
  881. } else {
  882. selection.active=false;
  883. }
  884. update();
  885. }
  886. } else {
  887. //if sel active and dblick last time < something
  888. //else
  889. selection.active=false;
  890. selection.selecting_mode=Selection::MODE_POINTER;
  891. selection.selecting_line=row;
  892. selection.selecting_column=col;
  893. }
  894. if (!mb.doubleclick && (OS::get_singleton()->get_ticks_msec()-last_dblclk)<600 && cursor.line==prev_line) {
  895. //tripleclick select line
  896. select(cursor.line,0,cursor.line,text[cursor.line].length());
  897. selection.selecting_column=0;
  898. last_dblclk=0;
  899. } else if (mb.doubleclick && text[cursor.line].length()) {
  900. //doubleclick select world
  901. String s = text[cursor.line];
  902. int beg=CLAMP(cursor.column,0,s.length());
  903. int end=beg;
  904. if (s[beg]>32 || beg==s.length()) {
  905. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  906. while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) {
  907. beg--;
  908. }
  909. while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) {
  910. end++;
  911. }
  912. if (end<s.length())
  913. end+=1;
  914. select(cursor.line,beg,cursor.line,end);
  915. selection.selecting_column=beg;
  916. }
  917. last_dblclk = OS::get_singleton()->get_ticks_msec();
  918. }
  919. update();
  920. }
  921. } else {
  922. // notify to show soft keyboard
  923. notification(NOTIFICATION_FOCUS_ENTER);
  924. }
  925. } break;
  926. case InputEvent::MOUSE_MOTION: {
  927. const InputEventMouseMotion &mm=p_input_event.mouse_motion;
  928. if (mm.button_mask&BUTTON_MASK_LEFT) {
  929. int row,col;
  930. if (!_get_mouse_pos(Point2i(mm.x,mm.y), row,col))
  931. return;
  932. if (selection.selecting_mode!=Selection::MODE_NONE) {
  933. select(selection.selecting_line,selection.selecting_column,row,col);
  934. cursor_set_line( row );
  935. cursor_set_column( col );
  936. update();
  937. }
  938. }
  939. } break;
  940. case InputEvent::KEY: {
  941. InputEventKey k=p_input_event.key;
  942. if (!k.pressed)
  943. return;
  944. if (completion_active) {
  945. if (readonly)
  946. break;
  947. bool valid=true;
  948. if (k.mod.command || k.mod.meta)
  949. valid=false;
  950. if (valid) {
  951. if (!k.mod.alt) {
  952. if (k.scancode==KEY_UP) {
  953. if (completion_index>0) {
  954. completion_index--;
  955. completion_current=completion_options[completion_index];
  956. update();
  957. }
  958. accept_event();
  959. return;
  960. }
  961. if (k.scancode==KEY_DOWN) {
  962. if (completion_index<completion_options.size()-1) {
  963. completion_index++;
  964. completion_current=completion_options[completion_index];
  965. update();
  966. }
  967. accept_event();
  968. return;
  969. }
  970. if (k.scancode==KEY_PAGEUP) {
  971. completion_index-=get_constant("completion_lines");
  972. if (completion_index<0)
  973. completion_index=0;
  974. completion_current=completion_options[completion_index];
  975. update();
  976. accept_event();
  977. return;
  978. }
  979. if (k.scancode==KEY_PAGEDOWN) {
  980. completion_index+=get_constant("completion_lines");
  981. if (completion_index>=completion_options.size())
  982. completion_index=completion_options.size()-1;
  983. completion_current=completion_options[completion_index];
  984. update();
  985. accept_event();
  986. return;
  987. }
  988. if (k.scancode==KEY_HOME && completion_index>0) {
  989. completion_index=0;
  990. completion_current=completion_options[completion_index];
  991. update();
  992. accept_event();
  993. return;
  994. }
  995. if (k.scancode==KEY_END && completion_index<completion_options.size()-1) {
  996. completion_index=completion_options.size()-1;
  997. completion_current=completion_options[completion_index];
  998. update();
  999. accept_event();
  1000. return;
  1001. }
  1002. if (k.scancode==KEY_DOWN) {
  1003. if (completion_index<completion_options.size()-1) {
  1004. completion_index++;
  1005. completion_current=completion_options[completion_index];
  1006. update();
  1007. }
  1008. accept_event();
  1009. return;
  1010. }
  1011. if (k.scancode==KEY_RETURN || k.scancode==KEY_TAB) {
  1012. _confirm_completion();
  1013. accept_event();
  1014. return;
  1015. }
  1016. if (k.scancode==KEY_BACKSPACE) {
  1017. backspace_at_cursor();
  1018. _update_completion_candidates();
  1019. accept_event();
  1020. return;
  1021. }
  1022. if (k.scancode==KEY_SHIFT) {
  1023. accept_event();
  1024. return;
  1025. }
  1026. }
  1027. if (k.unicode>32) {
  1028. if (cursor.column<text[cursor.line].length() && text[cursor.line][cursor.column]==k.unicode) {
  1029. //same char, move ahead
  1030. cursor_set_column(cursor.column+1);
  1031. } else {
  1032. //different char, go back
  1033. const CharType chr[2] = {(CharType)k.unicode, 0};
  1034. if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  1035. _consume_pair_symbol(chr[0]);
  1036. } else {
  1037. _insert_text_at_cursor(chr);
  1038. }
  1039. }
  1040. _update_completion_candidates();
  1041. accept_event();
  1042. return;
  1043. }
  1044. }
  1045. _cancel_completion();
  1046. }
  1047. /* TEST CONTROL FIRST!! */
  1048. // some remaps for duplicate functions..
  1049. if (k.mod.command && !k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode==KEY_INSERT) {
  1050. k.scancode=KEY_C;
  1051. }
  1052. if (!k.mod.command && k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode==KEY_INSERT) {
  1053. k.scancode=KEY_V;
  1054. k.mod.command=true;
  1055. k.mod.shift=false;
  1056. }
  1057. // stuff to do when selection is active..
  1058. if (selection.active) {
  1059. if (readonly)
  1060. break;
  1061. bool clear=false;
  1062. bool unselect=false;
  1063. bool dobreak=false;
  1064. switch(k.scancode) {
  1065. case KEY_TAB: {
  1066. String txt = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1067. String prev_txt=txt;
  1068. if (k.mod.shift) {
  1069. for(int i=0;i<txt.length();i++) {
  1070. if (((i>0 && txt[i-1]=='\n') || (i==0 /*&& selection.from_column==0*/)) && (txt[i]=='\t' || txt[i]==' ')) {
  1071. txt.remove(i);
  1072. //i--;
  1073. }
  1074. }
  1075. } else {
  1076. for(int i=0;i<txt.length();i++) {
  1077. if (((i>0 && txt[i-1]=='\n') || (i==0 /*&& selection.from_column==0*/))) {
  1078. txt=txt.insert(i,"\t");
  1079. //i--;
  1080. }
  1081. }
  1082. }
  1083. if (txt!=prev_txt) {
  1084. int sel_line=selection.from_line;
  1085. int sel_column=selection.from_column;
  1086. cursor_set_line(selection.from_line);
  1087. cursor_set_column(selection.from_column);
  1088. _begin_compex_operation();
  1089. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1090. _insert_text_at_cursor(txt);
  1091. _end_compex_operation();
  1092. selection.active=true;
  1093. selection.from_column=sel_column;
  1094. selection.from_line=sel_line;
  1095. selection.to_column=cursor.column;
  1096. selection.to_line=cursor.line;
  1097. update();
  1098. }
  1099. dobreak=true;
  1100. accept_event();
  1101. } break;
  1102. case KEY_X:
  1103. case KEY_C:
  1104. //special keys often used with control, wait...
  1105. clear=(!k.mod.command || k.mod.shift || k.mod.alt );
  1106. break;
  1107. case KEY_DELETE:
  1108. case KEY_BACKSPACE:
  1109. accept_event();
  1110. clear=true; dobreak=true;
  1111. break;
  1112. case KEY_LEFT:
  1113. case KEY_RIGHT:
  1114. case KEY_UP:
  1115. case KEY_DOWN:
  1116. case KEY_PAGEUP:
  1117. case KEY_PAGEDOWN:
  1118. case KEY_HOME:
  1119. case KEY_END:
  1120. // ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys)
  1121. if (k.mod.command || k.mod.shift || k.mod.alt)
  1122. break;
  1123. unselect=true;
  1124. break;
  1125. default:
  1126. if (k.unicode>=32 && !k.mod.command && !k.mod.alt && !k.mod.meta)
  1127. clear=true;
  1128. if (auto_brace_completion_enabled && _is_pair_left_symbol(k.unicode))
  1129. clear=false;
  1130. }
  1131. if (unselect) {
  1132. selection.active=false;
  1133. selection.selecting_mode=Selection::MODE_NONE;
  1134. update();
  1135. }
  1136. if (clear) {
  1137. selection.active=false;
  1138. update();
  1139. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1140. cursor_set_line(selection.from_line);
  1141. cursor_set_column(selection.from_column);
  1142. update();
  1143. }
  1144. if (dobreak)
  1145. break;
  1146. }
  1147. selection.selecting_text=false;
  1148. bool scancode_handled=true;
  1149. // special scancode test...
  1150. switch (k.scancode) {
  1151. case KEY_ENTER:
  1152. case KEY_RETURN: {
  1153. if (readonly)
  1154. break;
  1155. String ins="\n";
  1156. //keep indentation
  1157. for(int i=0;i<text[cursor.line].length();i++) {
  1158. if (text[cursor.line][i]=='\t')
  1159. ins+="\t";
  1160. else
  1161. break;
  1162. }
  1163. _insert_text_at_cursor(ins);
  1164. _push_current_op();
  1165. } break;
  1166. case KEY_ESCAPE: {
  1167. if (completion_hint!="") {
  1168. completion_hint="";
  1169. update();
  1170. }
  1171. } break;
  1172. case KEY_TAB: {
  1173. if (readonly)
  1174. break;
  1175. if (selection.active) {
  1176. } else {
  1177. if (k.mod.shift) {
  1178. int cc = cursor.column;
  1179. if (cc>0 && cc<=text[cursor.line].length() && text[cursor.line][cursor.column-1]=='\t') {
  1180. //simple unindent
  1181. backspace_at_cursor();
  1182. }
  1183. } else {
  1184. //simple indent
  1185. _insert_text_at_cursor("\t");
  1186. }
  1187. }
  1188. } break;
  1189. case KEY_BACKSPACE: {
  1190. if (readonly)
  1191. break;
  1192. backspace_at_cursor();
  1193. } break;
  1194. case KEY_LEFT: {
  1195. if (k.mod.shift)
  1196. _pre_shift_selection();
  1197. #ifdef APPLE_STYLE_KEYS
  1198. if (k.mod.command) {
  1199. cursor_set_column(0);
  1200. } else if (k.mod.alt) {
  1201. #else
  1202. if (k.mod.alt) {
  1203. scancode_handled=false;
  1204. break;
  1205. } else if (k.mod.command) {
  1206. #endif
  1207. bool prev_char=false;
  1208. int cc=cursor.column;
  1209. while (cc>0) {
  1210. bool ischar=_is_text_char(text[cursor.line][cc-1]);
  1211. if (prev_char && !ischar)
  1212. break;
  1213. prev_char=ischar;
  1214. cc--;
  1215. }
  1216. cursor_set_column(cc);
  1217. } else if (cursor.column==0) {
  1218. if (cursor.line>0) {
  1219. cursor_set_line(cursor.line-1);
  1220. cursor_set_column(text[cursor.line].length());
  1221. }
  1222. } else {
  1223. cursor_set_column(cursor_get_column()-1);
  1224. }
  1225. if (k.mod.shift)
  1226. _post_shift_selection();
  1227. } break;
  1228. case KEY_RIGHT: {
  1229. if (k.mod.shift)
  1230. _pre_shift_selection();
  1231. #ifdef APPLE_STYLE_KEYS
  1232. if (k.mod.command) {
  1233. cursor_set_column(text[cursor.line].length());
  1234. } else if (k.mod.alt) {
  1235. #else
  1236. if (k.mod.alt) {
  1237. scancode_handled=false;
  1238. break;
  1239. } else if (k.mod.command) {
  1240. #endif
  1241. bool prev_char=false;
  1242. int cc=cursor.column;
  1243. while (cc<text[cursor.line].length()) {
  1244. bool ischar=_is_text_char(text[cursor.line][cc]);
  1245. if (prev_char && !ischar)
  1246. break;
  1247. prev_char=ischar;
  1248. cc++;
  1249. }
  1250. cursor_set_column(cc);
  1251. } else if (cursor.column==text[cursor.line].length()) {
  1252. if (cursor.line<text.size()-1) {
  1253. cursor_set_line(cursor.line+1);
  1254. cursor_set_column(0);
  1255. }
  1256. } else {
  1257. cursor_set_column(cursor_get_column()+1);
  1258. }
  1259. if (k.mod.shift)
  1260. _post_shift_selection();
  1261. } break;
  1262. case KEY_UP: {
  1263. if (k.mod.shift)
  1264. _pre_shift_selection();
  1265. if (k.mod.alt) {
  1266. scancode_handled=false;
  1267. break;
  1268. }
  1269. #ifdef APPLE_STYLE_KEYS
  1270. if (k.mod.command)
  1271. cursor_set_line(0);
  1272. else
  1273. #endif
  1274. cursor_set_line(cursor_get_line()-1);
  1275. if (k.mod.shift)
  1276. _post_shift_selection();
  1277. _cancel_code_hint();
  1278. } break;
  1279. case KEY_DOWN: {
  1280. if (k.mod.shift)
  1281. _pre_shift_selection();
  1282. if (k.mod.alt) {
  1283. scancode_handled=false;
  1284. break;
  1285. }
  1286. #ifdef APPLE_STYLE_KEYS
  1287. if (k.mod.command)
  1288. cursor_set_line(text.size()-1);
  1289. else
  1290. #endif
  1291. cursor_set_line(cursor_get_line()+1);
  1292. if (k.mod.shift)
  1293. _post_shift_selection();
  1294. _cancel_code_hint();
  1295. } break;
  1296. case KEY_DELETE: {
  1297. if (readonly)
  1298. break;
  1299. int curline_len = text[cursor.line].length();
  1300. if (cursor.line==text.size()-1 && cursor.column==curline_len)
  1301. break; //nothing to do
  1302. int next_line = cursor.column<curline_len?cursor.line:cursor.line+1;
  1303. int next_column = cursor.column<curline_len?(cursor.column+1):0;
  1304. _remove_text(cursor.line,cursor.column,next_line,next_column);
  1305. update();
  1306. } break;
  1307. #ifdef APPLE_STYLE_KEYS
  1308. case KEY_HOME: {
  1309. if (k.mod.shift)
  1310. _pre_shift_selection();
  1311. cursor_set_line(0);
  1312. if (k.mod.shift)
  1313. _post_shift_selection();
  1314. } break;
  1315. case KEY_END: {
  1316. if (k.mod.shift)
  1317. _pre_shift_selection();
  1318. cursor_set_line(text.size()-1);
  1319. if (k.mod.shift)
  1320. _post_shift_selection();
  1321. } break;
  1322. #else
  1323. case KEY_HOME: {
  1324. if (k.mod.shift)
  1325. _pre_shift_selection();
  1326. // compute whitespace symbols seq length
  1327. int current_line_whitespace_len = 0;
  1328. while(current_line_whitespace_len < text[cursor.line].length()) {
  1329. CharType c = text[cursor.line][current_line_whitespace_len];
  1330. if(c != '\t' && c != ' ')
  1331. break;
  1332. current_line_whitespace_len++;
  1333. }
  1334. if(cursor_get_column() == current_line_whitespace_len)
  1335. cursor_set_column(0);
  1336. else
  1337. cursor_set_column(current_line_whitespace_len);
  1338. if (k.mod.command)
  1339. cursor_set_line(0);
  1340. if (k.mod.shift)
  1341. _post_shift_selection();
  1342. _cancel_completion();
  1343. completion_hint="";
  1344. } break;
  1345. case KEY_END: {
  1346. if (k.mod.shift)
  1347. _pre_shift_selection();
  1348. if (k.mod.command)
  1349. cursor_set_line(text.size()-1);
  1350. cursor_set_column(text[cursor.line].length());
  1351. if (k.mod.shift)
  1352. _post_shift_selection();
  1353. _cancel_completion();
  1354. completion_hint="";
  1355. } break;
  1356. #endif
  1357. case KEY_PAGEUP: {
  1358. if (k.mod.shift)
  1359. _pre_shift_selection();
  1360. cursor_set_line(cursor_get_line()-get_visible_rows());
  1361. if (k.mod.shift)
  1362. _post_shift_selection();
  1363. _cancel_completion();
  1364. completion_hint="";
  1365. } break;
  1366. case KEY_PAGEDOWN: {
  1367. if (k.mod.shift)
  1368. _pre_shift_selection();
  1369. cursor_set_line(cursor_get_line()+get_visible_rows());
  1370. if (k.mod.shift)
  1371. _post_shift_selection();
  1372. _cancel_completion();
  1373. completion_hint="";
  1374. } break;
  1375. case KEY_A: {
  1376. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1377. scancode_handled=false;
  1378. break;
  1379. }
  1380. select_all();
  1381. } break;
  1382. case KEY_X: {
  1383. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1384. scancode_handled=false;
  1385. break;
  1386. }
  1387. if (!selection.active){
  1388. String clipboard = text[cursor.line];
  1389. OS::get_singleton()->set_clipboard(clipboard);
  1390. cursor_set_line(cursor.line);
  1391. cursor_set_column(0);
  1392. _remove_text(cursor.line,0,cursor.line,text[cursor.line].length());
  1393. backspace_at_cursor();
  1394. update();
  1395. cursor_set_line(cursor.line+1);
  1396. cut_copy_line = true;
  1397. }
  1398. else
  1399. {
  1400. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1401. OS::get_singleton()->set_clipboard(clipboard);
  1402. cursor_set_line(selection.from_line);
  1403. cursor_set_column(selection.from_column);
  1404. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1405. selection.active=false;
  1406. selection.selecting_mode=Selection::MODE_NONE;
  1407. update();
  1408. cut_copy_line = false;
  1409. }
  1410. } break;
  1411. case KEY_C: {
  1412. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1413. scancode_handled=false;
  1414. break;
  1415. }
  1416. if (!selection.active){
  1417. String clipboard = _base_get_text(cursor.line,0,cursor.line,text[cursor.line].length());
  1418. OS::get_singleton()->set_clipboard(clipboard);
  1419. cut_copy_line = true;
  1420. }
  1421. else{
  1422. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1423. OS::get_singleton()->set_clipboard(clipboard);
  1424. cut_copy_line = false;
  1425. }
  1426. } break;
  1427. case KEY_Z: {
  1428. if (!k.mod.command) {
  1429. scancode_handled=false;
  1430. break;
  1431. }
  1432. if (k.mod.shift)
  1433. redo();
  1434. else
  1435. undo();
  1436. } break;
  1437. case KEY_V: {
  1438. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1439. scancode_handled=false;
  1440. break;
  1441. }
  1442. String clipboard = OS::get_singleton()->get_clipboard();
  1443. if (selection.active) {
  1444. selection.active=false;
  1445. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1446. cursor_set_line(selection.from_line);
  1447. cursor_set_column(selection.from_column);
  1448. }
  1449. else if (cut_copy_line)
  1450. {
  1451. cursor_set_column(0);
  1452. String ins="\n";
  1453. clipboard += ins;
  1454. }
  1455. _insert_text_at_cursor(clipboard);
  1456. update();
  1457. } break;
  1458. case KEY_SPACE: {
  1459. #ifdef OSX_ENABLED
  1460. if (completion_enabled && k.mod.meta) { //cmd-space is spotlight shortcut in OSX
  1461. #else
  1462. if (completion_enabled && k.mod.command) {
  1463. #endif
  1464. query_code_comple();
  1465. scancode_handled=true;
  1466. } else {
  1467. scancode_handled=false;
  1468. }
  1469. } break;
  1470. case KEY_U:{
  1471. if (!k.mod.command || k.mod.shift) {
  1472. scancode_handled=false;
  1473. break;
  1474. }
  1475. else {
  1476. if (selection.active) {
  1477. int ini = selection.from_line;
  1478. int end = selection.to_line;
  1479. for (int i=ini; i<= end; i++)
  1480. {
  1481. if (text[i][0] == '#')
  1482. _remove_text(i,0,i,1);
  1483. }
  1484. }
  1485. else{
  1486. if (text[cursor.line][0] == '#')
  1487. _remove_text(cursor.line,0,cursor.line,1);
  1488. }
  1489. update();
  1490. }
  1491. break;}
  1492. default: {
  1493. scancode_handled=false;
  1494. } break;
  1495. }
  1496. if (scancode_handled)
  1497. accept_event();
  1498. /*
  1499. if (!scancode_handled && !k.mod.command && !k.mod.alt) {
  1500. if (k.unicode>=32) {
  1501. if (readonly)
  1502. break;
  1503. accept_event();
  1504. } else {
  1505. break;
  1506. }
  1507. }
  1508. */
  1509. if (!scancode_handled && !k.mod.command) { //for german kbds
  1510. if (k.unicode>=32) {
  1511. if (readonly)
  1512. break;
  1513. const CharType chr[2] = {(CharType)k.unicode, 0};
  1514. if (completion_hint!="" && k.unicode==')') {
  1515. completion_hint="";
  1516. }
  1517. if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  1518. _consume_pair_symbol(chr[0]);
  1519. } else {
  1520. _insert_text_at_cursor(chr);
  1521. }
  1522. accept_event();
  1523. } else {
  1524. break;
  1525. }
  1526. }
  1527. return;
  1528. } break;
  1529. }
  1530. }
  1531. void TextEdit::_pre_shift_selection() {
  1532. if (!selection.active || selection.selecting_mode==Selection::MODE_NONE) {
  1533. selection.selecting_line=cursor.line;
  1534. selection.selecting_column=cursor.column;
  1535. selection.active=true;
  1536. }
  1537. selection.selecting_mode=Selection::MODE_SHIFT;
  1538. }
  1539. void TextEdit::_post_shift_selection() {
  1540. if (selection.active && selection.selecting_mode==Selection::MODE_SHIFT) {
  1541. select(selection.selecting_line,selection.selecting_column,cursor.line,cursor.column);
  1542. update();
  1543. }
  1544. selection.selecting_text=true;
  1545. }
  1546. /**** TEXT EDIT CORE API ****/
  1547. void TextEdit::_base_insert_text(int p_line, int p_char,const String& p_text,int &r_end_line,int &r_end_column) {
  1548. //save for undo...
  1549. ERR_FAIL_INDEX(p_line,text.size());
  1550. ERR_FAIL_COND(p_char<0);
  1551. /* STEP 1 add spaces if the char is greater than the end of the line */
  1552. while(p_char>text[p_line].length()) {
  1553. text.set(p_line,text[p_line]+String::chr(' '));
  1554. }
  1555. /* STEP 2 separate dest string in pre and post text */
  1556. String preinsert_text = text[p_line].substr(0,p_char);
  1557. String postinsert_text = text[p_line].substr(p_char,text[p_line].size());
  1558. /* STEP 3 remove \r from source text and separate in substrings */
  1559. //buh bye \r and split
  1560. Vector<String> substrings = p_text.replace("\r","").split("\n");
  1561. for(int i=0;i<substrings.size();i++) {
  1562. //insert the substrings
  1563. if (i==0) {
  1564. text.set(p_line,preinsert_text+substrings[i]);
  1565. } else {
  1566. text.insert(p_line+i,substrings[i]);
  1567. }
  1568. if (i==substrings.size()-1){
  1569. text.set(p_line+i,text[p_line+i]+postinsert_text);
  1570. }
  1571. }
  1572. r_end_line=p_line+substrings.size()-1;
  1573. r_end_column=text[r_end_line].length()-postinsert_text.length();
  1574. if (!text_changed_dirty && !setting_text) {
  1575. if (is_inside_tree())
  1576. MessageQueue::get_singleton()->push_call(this,"_text_changed_emit");
  1577. text_changed_dirty=true;
  1578. }
  1579. }
  1580. String TextEdit::_base_get_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) const {
  1581. ERR_FAIL_INDEX_V(p_from_line,text.size(),String());
  1582. ERR_FAIL_INDEX_V(p_from_column,text[p_from_line].length()+1,String());
  1583. ERR_FAIL_INDEX_V(p_to_line,text.size(),String());
  1584. ERR_FAIL_INDEX_V(p_to_column,text[p_to_line].length()+1,String());
  1585. ERR_FAIL_COND_V(p_to_line < p_from_line ,String()); // from > to
  1586. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column<p_from_column,String()); // from > to
  1587. String ret;
  1588. for(int i=p_from_line;i<=p_to_line;i++) {
  1589. int begin = (i==p_from_line)?p_from_column:0;
  1590. int end = (i==p_to_line)?p_to_column:text[i].length();
  1591. if (i>p_from_line)
  1592. ret+="\n";
  1593. ret+=text[i].substr(begin,end-begin);
  1594. }
  1595. return ret;
  1596. }
  1597. void TextEdit::_base_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) {
  1598. ERR_FAIL_INDEX(p_from_line,text.size());
  1599. ERR_FAIL_INDEX(p_from_column,text[p_from_line].length()+1);
  1600. ERR_FAIL_INDEX(p_to_line,text.size());
  1601. ERR_FAIL_INDEX(p_to_column,text[p_to_line].length()+1);
  1602. ERR_FAIL_COND(p_to_line < p_from_line ); // from > to
  1603. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column<p_from_column); // from > to
  1604. String pre_text = text[p_from_line].substr(0,p_from_column);
  1605. String post_text = text[p_to_line].substr(p_to_column,text[p_to_line].length());
  1606. for(int i=p_from_line;i<p_to_line;i++) {
  1607. text.remove(p_from_line+1);
  1608. }
  1609. text.set(p_from_line,pre_text+post_text);
  1610. if (!text_changed_dirty && !setting_text) {
  1611. if (is_inside_tree())
  1612. MessageQueue::get_singleton()->push_call(this,"_text_changed_emit");
  1613. text_changed_dirty=true;
  1614. }
  1615. }
  1616. void TextEdit::_insert_text(int p_line, int p_char,const String& p_text,int *r_end_line,int *r_end_column) {
  1617. if (!setting_text)
  1618. idle_detect->start();
  1619. if (undo_enabled) {
  1620. _clear_redo();
  1621. }
  1622. int retline,retchar;
  1623. _base_insert_text(p_line,p_char,p_text,retline,retchar);
  1624. if (r_end_line)
  1625. *r_end_line=retline;
  1626. if (r_end_column)
  1627. *r_end_column=retchar;
  1628. if (!undo_enabled)
  1629. return;
  1630. /* UNDO!! */
  1631. TextOperation op;
  1632. op.type=TextOperation::TYPE_INSERT;
  1633. op.from_line=p_line;
  1634. op.from_column=p_char;
  1635. op.to_line=retline;
  1636. op.to_column=retchar;
  1637. op.text=p_text;
  1638. op.version=++version;
  1639. op.chain_forward=false;
  1640. op.chain_backward=false;
  1641. //see if it shold just be set as current op
  1642. if (current_op.type!=op.type) {
  1643. _push_current_op();
  1644. current_op=op;
  1645. return; //set as current op, return
  1646. }
  1647. //see if it can be merged
  1648. if (current_op.to_line!=p_line || current_op.to_column!=p_char) {
  1649. _push_current_op();
  1650. current_op=op;
  1651. return; //set as current op, return
  1652. }
  1653. //merge current op
  1654. current_op.text+=p_text;
  1655. current_op.to_column=retchar;
  1656. current_op.to_line=retline;
  1657. current_op.version=op.version;
  1658. }
  1659. void TextEdit::_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) {
  1660. if (!setting_text)
  1661. idle_detect->start();
  1662. String text;
  1663. if (undo_enabled) {
  1664. _clear_redo();
  1665. text=_base_get_text(p_from_line,p_from_column,p_to_line,p_to_column);
  1666. }
  1667. _base_remove_text(p_from_line,p_from_column,p_to_line,p_to_column);
  1668. if (!undo_enabled)
  1669. return;
  1670. /* UNDO!! */
  1671. TextOperation op;
  1672. op.type=TextOperation::TYPE_REMOVE;
  1673. op.from_line=p_from_line;
  1674. op.from_column=p_from_column;
  1675. op.to_line=p_to_line;
  1676. op.to_column=p_to_column;
  1677. op.text=text;
  1678. op.version=++version;
  1679. op.chain_forward=false;
  1680. op.chain_backward=false;
  1681. //see if it shold just be set as current op
  1682. if (current_op.type!=op.type) {
  1683. _push_current_op();
  1684. current_op=op;
  1685. return; //set as current op, return
  1686. }
  1687. //see if it can be merged
  1688. if (current_op.from_line==p_to_line && current_op.from_column==p_to_column) {
  1689. //basckace or similar
  1690. current_op.text=text+current_op.text;
  1691. current_op.from_line=p_from_line;
  1692. current_op.from_column=p_from_column;
  1693. return; //update current op
  1694. }
  1695. if (current_op.from_line==p_from_line && current_op.from_column==p_from_column) {
  1696. //current_op.text=text+current_op.text;
  1697. //current_op.from_line=p_from_line;
  1698. //current_op.from_column=p_from_column;
  1699. //return; //update current op
  1700. }
  1701. _push_current_op();
  1702. current_op=op;
  1703. }
  1704. void TextEdit::_insert_text_at_cursor(const String& p_text) {
  1705. int new_column,new_line;
  1706. _insert_text(cursor.line,cursor.column,p_text,&new_line,&new_column);
  1707. cursor_set_line(new_line);
  1708. cursor_set_column(new_column);
  1709. update();
  1710. }
  1711. int TextEdit::get_char_count() {
  1712. int totalsize=0;
  1713. for (int i=0;i<text.size();i++) {
  1714. if (i>0)
  1715. totalsize++; // incliude \n
  1716. totalsize+=text[i].length();
  1717. }
  1718. return totalsize; // omit last \n
  1719. }
  1720. Size2 TextEdit::get_minimum_size() {
  1721. return cache.style_normal->get_minimum_size();
  1722. }
  1723. int TextEdit::get_visible_rows() const {
  1724. int total=cache.size.height;
  1725. total-=cache.style_normal->get_minimum_size().height;
  1726. total/=get_row_height();
  1727. return total;
  1728. }
  1729. void TextEdit::adjust_viewport_to_cursor() {
  1730. if (cursor.line_ofs>cursor.line)
  1731. cursor.line_ofs=cursor.line;
  1732. int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w;
  1733. if (v_scroll->is_visible())
  1734. visible_width-=v_scroll->get_combined_minimum_size().width;
  1735. visible_width-=20; // give it a little more space
  1736. //printf("rowofs %i, visrows %i, cursor.line %i\n",cursor.line_ofs,get_visible_rows(),cursor.line);
  1737. int visible_rows = get_visible_rows();
  1738. if (h_scroll->is_visible())
  1739. visible_rows-=((h_scroll->get_combined_minimum_size().height-1)/get_row_height());
  1740. if (cursor.line>=(cursor.line_ofs+visible_rows))
  1741. cursor.line_ofs=cursor.line-visible_rows+1;
  1742. if (cursor.line<cursor.line_ofs)
  1743. cursor.line_ofs=cursor.line;
  1744. int cursor_x = get_column_x_offset( cursor.column, text[cursor.line] );
  1745. if (cursor_x>(cursor.x_ofs+visible_width))
  1746. cursor.x_ofs=cursor_x-visible_width+1;
  1747. if (cursor_x < cursor.x_ofs)
  1748. cursor.x_ofs=cursor_x;
  1749. update();
  1750. /*
  1751. get_range()->set_max(text.size());
  1752. get_range()->set_page(get_visible_rows());
  1753. get_range()->set((int)cursor.line_ofs);
  1754. */
  1755. }
  1756. void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
  1757. if (p_col<0)
  1758. p_col=0;
  1759. cursor.column=p_col;
  1760. if (cursor.column > get_line( cursor.line ).length())
  1761. cursor.column=get_line( cursor.line ).length();
  1762. cursor.last_fit_x=get_column_x_offset(cursor.column,get_line(cursor.line));
  1763. if (p_adjust_viewport)
  1764. adjust_viewport_to_cursor();
  1765. if (!cursor_changed_dirty) {
  1766. if (is_inside_tree())
  1767. MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit");
  1768. cursor_changed_dirty=true;
  1769. }
  1770. }
  1771. void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport) {
  1772. if (setting_row)
  1773. return;
  1774. setting_row=true;
  1775. if (p_row<0)
  1776. p_row=0;
  1777. if (p_row>=(int)text.size())
  1778. p_row=(int)text.size()-1;
  1779. cursor.line=p_row;
  1780. cursor.column=get_char_pos_for( cursor.last_fit_x, get_line( cursor.line) );
  1781. if (p_adjust_viewport)
  1782. adjust_viewport_to_cursor();
  1783. setting_row=false;
  1784. if (!cursor_changed_dirty) {
  1785. if (is_inside_tree())
  1786. MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit");
  1787. cursor_changed_dirty=true;
  1788. }
  1789. }
  1790. int TextEdit::cursor_get_column() const {
  1791. return cursor.column;
  1792. }
  1793. int TextEdit::cursor_get_line() const {
  1794. return cursor.line;
  1795. }
  1796. void TextEdit::_scroll_moved(double p_to_val) {
  1797. if (updating_scrolls)
  1798. return;
  1799. if (h_scroll->is_visible())
  1800. cursor.x_ofs=h_scroll->get_val();
  1801. if (v_scroll->is_visible())
  1802. cursor.line_ofs=v_scroll->get_val();
  1803. update();
  1804. }
  1805. int TextEdit::get_row_height() const {
  1806. return cache.font->get_height()+cache.line_spacing;
  1807. }
  1808. int TextEdit::get_char_pos_for(int p_px,String p_str) const {
  1809. int px=0;
  1810. int c=0;
  1811. int tab_w = cache.font->get_char_size(' ').width*tab_size;
  1812. while (c<p_str.length()) {
  1813. int w=0;
  1814. if (p_str[c]=='\t') {
  1815. int left = px%tab_w;
  1816. if (left==0)
  1817. w=tab_w;
  1818. else
  1819. w=tab_w-px%tab_w; // is right...
  1820. } else {
  1821. w=cache.font->get_char_size(p_str[c],p_str[c+1]).width;
  1822. }
  1823. if (p_px<(px+w/2))
  1824. break;
  1825. px+=w;
  1826. c++;
  1827. }
  1828. return c;
  1829. }
  1830. int TextEdit::get_column_x_offset(int p_char,String p_str) {
  1831. int px=0;
  1832. int tab_w = cache.font->get_char_size(' ').width*tab_size;
  1833. for (int i=0;i<p_char;i++) {
  1834. if (i>=p_str.length())
  1835. break;
  1836. if (p_str[i]=='\t') {
  1837. int left = px%tab_w;
  1838. if (left==0)
  1839. px+=tab_w;
  1840. else
  1841. px+=tab_w-px%tab_w; // is right...
  1842. } else {
  1843. px+=cache.font->get_char_size(p_str[i],p_str[i+1]).width;
  1844. }
  1845. }
  1846. return px;
  1847. }
  1848. void TextEdit::insert_text_at_cursor(const String& p_text) {
  1849. if (selection.active) {
  1850. cursor_set_line(selection.from_line);
  1851. cursor_set_column(selection.from_column);
  1852. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1853. selection.active=false;
  1854. selection.selecting_mode=Selection::MODE_NONE;
  1855. }
  1856. _insert_text_at_cursor(p_text);
  1857. update();
  1858. }
  1859. Control::CursorShape TextEdit::get_cursor_shape(const Point2& p_pos) const {
  1860. if(completion_active && completion_rect.has_point(p_pos)) {
  1861. return CURSOR_ARROW;
  1862. }
  1863. return CURSOR_IBEAM;
  1864. }
  1865. void TextEdit::set_text(String p_text){
  1866. setting_text=true;
  1867. clear();
  1868. _insert_text_at_cursor(p_text);
  1869. clear_undo_history();
  1870. cursor.column=0;
  1871. cursor.line=0;
  1872. cursor.x_ofs=0;
  1873. cursor.line_ofs=0;
  1874. cursor.last_fit_x=0;
  1875. cursor_set_line(0);
  1876. cursor_set_column(0);
  1877. update();
  1878. setting_text=false;
  1879. //get_range()->set(0);
  1880. };
  1881. String TextEdit::get_text() {
  1882. String longthing;
  1883. int len = text.size();
  1884. for (int i=0;i<len;i++) {
  1885. longthing+=text[i];
  1886. if (i!=len-1)
  1887. longthing+="\n";
  1888. }
  1889. return longthing;
  1890. };
  1891. String TextEdit::get_text_for_completion() {
  1892. String longthing;
  1893. int len = text.size();
  1894. for (int i=0;i<len;i++) {
  1895. if (i==cursor.line) {
  1896. longthing+=text[i].substr(0,cursor.column);
  1897. longthing+=String::chr(0xFFFF); //not unicode, represents the cursor
  1898. longthing+=text[i].substr(cursor.column,text[i].size());
  1899. } else {
  1900. longthing+=text[i];
  1901. }
  1902. if (i!=len-1)
  1903. longthing+="\n";
  1904. }
  1905. return longthing;
  1906. };
  1907. String TextEdit::get_line(int line) const {
  1908. if (line<0 || line>=text.size())
  1909. return "";
  1910. return text[line];
  1911. };
  1912. void TextEdit::_clear() {
  1913. clear_undo_history();
  1914. text.clear();
  1915. cursor.column=0;
  1916. cursor.line=0;
  1917. cursor.x_ofs=0;
  1918. cursor.line_ofs=0;
  1919. cursor.last_fit_x=0;
  1920. }
  1921. void TextEdit::clear() {
  1922. setting_text=true;
  1923. _clear();
  1924. setting_text=false;
  1925. };
  1926. void TextEdit::set_readonly(bool p_readonly) {
  1927. readonly=p_readonly;
  1928. }
  1929. void TextEdit::set_wrap(bool p_wrap) {
  1930. wrap=p_wrap;
  1931. }
  1932. void TextEdit::set_max_chars(int p_max_chars) {
  1933. max_chars=p_max_chars;
  1934. }
  1935. void TextEdit::_update_caches() {
  1936. cache.style_normal=get_stylebox("normal");
  1937. cache.style_focus=get_stylebox("focus");
  1938. cache.font=get_font("font");
  1939. cache.font_color=get_color("font_color");
  1940. cache.font_selected_color=get_color("font_selected_color");
  1941. cache.keyword_color=get_color("keyword_color");
  1942. cache.selection_color=get_color("selection_color");
  1943. cache.mark_color=get_color("mark_color");
  1944. cache.current_line_color=get_color("current_line_color");
  1945. cache.breakpoint_color=get_color("breakpoint_color");
  1946. cache.brace_mismatch_color=get_color("brace_mismatch_color");
  1947. cache.line_spacing=get_constant("line_spacing");
  1948. cache.row_height = cache.font->get_height() + cache.line_spacing;
  1949. cache.tab_icon=get_icon("tab");
  1950. text.set_font(cache.font);
  1951. }
  1952. void TextEdit::clear_colors() {
  1953. keywords.clear();
  1954. color_regions.clear();;
  1955. text.clear_caches();
  1956. custom_bg_color=Color(0,0,0,0);
  1957. }
  1958. void TextEdit::set_custom_bg_color(const Color& p_color) {
  1959. custom_bg_color=p_color;
  1960. update();
  1961. }
  1962. void TextEdit::add_keyword_color(const String& p_keyword,const Color& p_color) {
  1963. keywords[p_keyword]=p_color;
  1964. update();
  1965. }
  1966. void TextEdit::add_color_region(const String& p_begin_key,const String& p_end_key,const Color &p_color,bool p_line_only) {
  1967. color_regions.push_back(ColorRegion(p_begin_key,p_end_key,p_color,p_line_only));
  1968. text.clear_caches();
  1969. update();
  1970. }
  1971. void TextEdit::set_symbol_color(const Color& p_color) {
  1972. symbol_color=p_color;
  1973. update();
  1974. }
  1975. void TextEdit::set_syntax_coloring(bool p_enabled) {
  1976. syntax_coloring=p_enabled;
  1977. update();
  1978. }
  1979. bool TextEdit::is_syntax_coloring_enabled() const {
  1980. return syntax_coloring;
  1981. }
  1982. void TextEdit::cut() {
  1983. if (!selection.active)
  1984. return;
  1985. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1986. OS::get_singleton()->set_clipboard(clipboard);
  1987. cursor_set_line(selection.from_line);
  1988. cursor_set_column(selection.from_column);
  1989. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1990. selection.active=false;
  1991. selection.selecting_mode=Selection::MODE_NONE;
  1992. update();
  1993. }
  1994. void TextEdit::copy() {
  1995. if (!selection.active)
  1996. return;
  1997. print_line("from line: "+itos(selection.from_line));
  1998. print_line("from column: "+itos(selection.from_column));
  1999. print_line("to line: "+itos(selection.to_line));
  2000. print_line("to column: "+itos(selection.to_column));
  2001. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2002. OS::get_singleton()->set_clipboard(clipboard);
  2003. }
  2004. void TextEdit::paste() {
  2005. if (selection.active) {
  2006. cursor_set_line(selection.from_line);
  2007. cursor_set_column(selection.from_column);
  2008. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2009. selection.active=false;
  2010. selection.selecting_mode=Selection::MODE_NONE;
  2011. }
  2012. String clipboard = OS::get_singleton()->get_clipboard();
  2013. _insert_text_at_cursor(clipboard);
  2014. update();
  2015. }
  2016. void TextEdit::select_all() {
  2017. if (text.size()==1 && text[0].length()==0)
  2018. return;
  2019. selection.active=true;
  2020. selection.from_line=0;
  2021. selection.from_column=0;
  2022. selection.selecting_line=0;
  2023. selection.selecting_column=0;
  2024. selection.to_line=text.size()-1;
  2025. selection.to_column=text[selection.to_line].length();
  2026. selection.selecting_mode=Selection::MODE_SHIFT;
  2027. selection.shiftclick_left=true;
  2028. cursor_set_line( selection.to_line, false );
  2029. cursor_set_column( selection.to_column, false );
  2030. update();
  2031. }
  2032. void TextEdit::deselect() {
  2033. selection.active=false;
  2034. update();
  2035. }
  2036. void TextEdit::select(int p_from_line,int p_from_column,int p_to_line,int p_to_column) {
  2037. if (p_from_line>=text.size())
  2038. p_from_line=text.size()-1;
  2039. if (p_from_column>=text[p_from_line].length())
  2040. p_from_column=text[p_from_line].length();
  2041. if (p_to_line>=text.size())
  2042. p_to_line=text.size()-1;
  2043. if (p_to_column>=text[p_to_line].length())
  2044. p_to_column=text[p_to_line].length();
  2045. selection.from_line=p_from_line;
  2046. selection.from_column=p_from_column;
  2047. selection.to_line=p_to_line;
  2048. selection.to_column=p_to_column;
  2049. selection.active=true;
  2050. if (selection.from_line==selection.to_line) {
  2051. if (selection.from_column==selection.to_column) {
  2052. selection.active=false;
  2053. } else if (selection.from_column>selection.to_column) {
  2054. selection.shiftclick_left = false;
  2055. SWAP( selection.from_column, selection.to_column );
  2056. } else {
  2057. selection.shiftclick_left = true;
  2058. }
  2059. } else if (selection.from_line>selection.to_line) {
  2060. selection.shiftclick_left = false;
  2061. SWAP( selection.from_line, selection.to_line );
  2062. SWAP( selection.from_column, selection.to_column );
  2063. } else {
  2064. selection.shiftclick_left = true;
  2065. }
  2066. update();
  2067. }
  2068. bool TextEdit::is_selection_active() const {
  2069. return selection.active;
  2070. }
  2071. int TextEdit::get_selection_from_line() const {
  2072. ERR_FAIL_COND_V(!selection.active,-1);
  2073. return selection.from_line;
  2074. }
  2075. int TextEdit::get_selection_from_column() const {
  2076. ERR_FAIL_COND_V(!selection.active,-1);
  2077. return selection.from_column;
  2078. }
  2079. int TextEdit::get_selection_to_line() const {
  2080. ERR_FAIL_COND_V(!selection.active,-1);
  2081. return selection.to_line;
  2082. }
  2083. int TextEdit::get_selection_to_column() const {
  2084. ERR_FAIL_COND_V(!selection.active,-1);
  2085. return selection.to_column;
  2086. }
  2087. String TextEdit::get_selection_text() const {
  2088. if (!selection.active)
  2089. return "";
  2090. return _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  2091. }
  2092. String TextEdit::get_word_under_cursor() const {
  2093. int prev_cc = cursor.column;
  2094. while(prev_cc >0) {
  2095. bool is_char = _is_text_char(text[cursor.line][prev_cc-1]);
  2096. if (!is_char)
  2097. break;
  2098. --prev_cc;
  2099. }
  2100. int next_cc = cursor.column;
  2101. while(next_cc<text[cursor.line].length()) {
  2102. bool is_char = _is_text_char(text[cursor.line][next_cc]);
  2103. if(!is_char)
  2104. break;
  2105. ++ next_cc;
  2106. }
  2107. if (prev_cc == cursor.column || next_cc == cursor.column)
  2108. return "";
  2109. return text[cursor.line].substr(prev_cc, next_cc-prev_cc);
  2110. }
  2111. DVector<int> TextEdit::_search_bind(const String &p_key,uint32_t p_search_flags, int p_from_line,int p_from_column) const {
  2112. int col,line;
  2113. if (search(p_key,p_search_flags,p_from_line,p_from_column,col,line)) {
  2114. DVector<int> result;
  2115. result.resize(2);
  2116. result.set(0,line);
  2117. result.set(1,col);
  2118. return result;
  2119. } else {
  2120. return DVector<int>();
  2121. }
  2122. }
  2123. 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 {
  2124. if (p_key.length()==0)
  2125. return false;
  2126. ERR_FAIL_INDEX_V(p_from_line,text.size(),false);
  2127. ERR_FAIL_INDEX_V(p_from_column,text[p_from_line].length()+1,false);
  2128. //search through the whole documment, but start by current line
  2129. int line=-1;
  2130. int pos=-1;
  2131. line=p_from_line;
  2132. for(int i=0;i<text.size()+1;i++) {
  2133. //backwards is broken...
  2134. //int idx=(p_search_flags&SEARCH_BACKWARDS)?(text.size()-i):i; //do backwards seearch
  2135. if (line<0) {
  2136. line=text.size()-1;
  2137. }
  2138. if (line==text.size()) {
  2139. line=0;
  2140. }
  2141. String text_line = text[line];
  2142. int from_column=0;
  2143. if (line==p_from_line) {
  2144. if (i==text.size()) {
  2145. //wrapped
  2146. if (p_search_flags&SEARCH_BACKWARDS) {
  2147. text_line=text_line.substr(from_column,text_line.length());
  2148. from_column=text_line.length();
  2149. } else {
  2150. text_line=text_line.substr(0,from_column);
  2151. from_column=0;
  2152. }
  2153. } else {
  2154. from_column=p_from_column;
  2155. }
  2156. } else {
  2157. //text_line=text_line.substr(0,p_from_column); //wrap around for missing begining.
  2158. if (p_search_flags&SEARCH_BACKWARDS)
  2159. from_column=text_line.length()-1;
  2160. else
  2161. from_column=0;
  2162. }
  2163. pos=-1;
  2164. if (!(p_search_flags&SEARCH_BACKWARDS)) {
  2165. pos = (p_search_flags&SEARCH_MATCH_CASE)?text_line.find(p_key,from_column):text_line.findn(p_key,from_column);
  2166. } else {
  2167. pos = (p_search_flags&SEARCH_MATCH_CASE)?text_line.rfind(p_key,from_column):text_line.rfindn(p_key,from_column);
  2168. }
  2169. if (pos!=-1 && (p_search_flags&SEARCH_WHOLE_WORDS)) {
  2170. //validate for whole words
  2171. if (pos>0 && _is_text_char(text_line[pos-1]))
  2172. pos=-1;
  2173. else if (_is_text_char(text_line[pos+p_key.length()]))
  2174. pos=-1;
  2175. }
  2176. if (pos!=-1)
  2177. break;
  2178. if (p_search_flags&SEARCH_BACKWARDS)
  2179. line--;
  2180. else
  2181. line++;
  2182. }
  2183. if (pos==-1) {
  2184. r_line=-1;
  2185. r_column=-1;
  2186. return false;
  2187. }
  2188. r_line=line;
  2189. r_column=pos;
  2190. return true;
  2191. }
  2192. void TextEdit::_cursor_changed_emit() {
  2193. emit_signal("cursor_changed");
  2194. cursor_changed_dirty=false;
  2195. }
  2196. void TextEdit::_text_changed_emit() {
  2197. emit_signal("text_changed");
  2198. text_changed_dirty=false;
  2199. }
  2200. void TextEdit::set_line_as_marked(int p_line,bool p_marked) {
  2201. ERR_FAIL_INDEX(p_line,text.size());
  2202. text.set_marked(p_line,p_marked);
  2203. update();
  2204. }
  2205. bool TextEdit::is_line_set_as_breakpoint(int p_line) const {
  2206. ERR_FAIL_INDEX_V(p_line,text.size(),false);
  2207. return text.is_breakpoint(p_line);
  2208. }
  2209. void TextEdit::set_line_as_breakpoint(int p_line,bool p_breakpoint) {
  2210. ERR_FAIL_INDEX(p_line,text.size());
  2211. text.set_breakpoint(p_line,p_breakpoint);
  2212. update();
  2213. }
  2214. void TextEdit::get_breakpoints(List<int> *p_breakpoints) const {
  2215. for(int i=0;i<text.size();i++) {
  2216. if (text.is_breakpoint(i))
  2217. p_breakpoints->push_back(i);
  2218. }
  2219. }
  2220. int TextEdit::get_line_count() const {
  2221. return text.size();
  2222. }
  2223. void TextEdit::_do_text_op(const TextOperation& p_op, bool p_reverse) {
  2224. ERR_FAIL_COND(p_op.type==TextOperation::TYPE_NONE);
  2225. bool insert = p_op.type==TextOperation::TYPE_INSERT;
  2226. if (p_reverse)
  2227. insert=!insert;
  2228. if (insert) {
  2229. int check_line;
  2230. int check_column;
  2231. _base_insert_text(p_op.from_line,p_op.from_column,p_op.text,check_line,check_column);
  2232. ERR_FAIL_COND( check_line != p_op.to_line ); // BUG
  2233. ERR_FAIL_COND( check_column != p_op.to_column ); // BUG
  2234. } else {
  2235. _base_remove_text(p_op.from_line,p_op.from_column,p_op.to_line,p_op.to_column);
  2236. }
  2237. }
  2238. void TextEdit::_clear_redo() {
  2239. if (undo_stack_pos==NULL)
  2240. return; //nothing to clear
  2241. _push_current_op();
  2242. while (undo_stack_pos) {
  2243. List<TextOperation>::Element *elem = undo_stack_pos;
  2244. undo_stack_pos=undo_stack_pos->next();
  2245. undo_stack.erase(elem);
  2246. }
  2247. }
  2248. void TextEdit::undo() {
  2249. _push_current_op();
  2250. if (undo_stack_pos==NULL) {
  2251. if (!undo_stack.size())
  2252. return; //nothing to undo
  2253. undo_stack_pos=undo_stack.back();
  2254. } else if (undo_stack_pos==undo_stack.front())
  2255. return; // at the bottom of the undo stack
  2256. else
  2257. undo_stack_pos=undo_stack_pos->prev();
  2258. _do_text_op( undo_stack_pos->get(),true);
  2259. if(undo_stack_pos->get().chain_backward) {
  2260. do {
  2261. undo_stack_pos = undo_stack_pos->prev();
  2262. _do_text_op(undo_stack_pos->get(), true);
  2263. } while(!undo_stack_pos->get().chain_forward);
  2264. }
  2265. cursor_set_line(undo_stack_pos->get().from_line);
  2266. cursor_set_column(undo_stack_pos->get().from_column);
  2267. update();
  2268. }
  2269. void TextEdit::redo() {
  2270. _push_current_op();
  2271. if (undo_stack_pos==NULL)
  2272. return; //nothing to do.
  2273. _do_text_op(undo_stack_pos->get(), false);
  2274. if(undo_stack_pos->get().chain_forward) {
  2275. do {
  2276. undo_stack_pos=undo_stack_pos->next();
  2277. _do_text_op(undo_stack_pos->get(), false);
  2278. } while(!undo_stack_pos->get().chain_backward);
  2279. }
  2280. cursor_set_line(undo_stack_pos->get().from_line);
  2281. cursor_set_column(undo_stack_pos->get().from_column);
  2282. undo_stack_pos=undo_stack_pos->next();
  2283. update();
  2284. }
  2285. void TextEdit::clear_undo_history() {
  2286. saved_version=0;
  2287. current_op.type=TextOperation::TYPE_NONE;
  2288. undo_stack_pos=NULL;
  2289. undo_stack.clear();
  2290. }
  2291. void TextEdit::_begin_compex_operation() {
  2292. _push_current_op();
  2293. next_operation_is_complex=true;
  2294. }
  2295. void TextEdit::_end_compex_operation() {
  2296. _push_current_op();
  2297. ERR_FAIL_COND(undo_stack.size() == 0);
  2298. if(undo_stack.back()->get().chain_forward) {
  2299. undo_stack.back()->get().chain_forward=false;
  2300. return;
  2301. }
  2302. undo_stack.back()->get().chain_backward=true;
  2303. }
  2304. void TextEdit::_push_current_op() {
  2305. if (current_op.type==TextOperation::TYPE_NONE)
  2306. return; // do nothing
  2307. if(next_operation_is_complex) {
  2308. current_op.chain_forward=true;
  2309. next_operation_is_complex=false;
  2310. }
  2311. undo_stack.push_back(current_op);
  2312. current_op.type=TextOperation::TYPE_NONE;
  2313. current_op.text="";
  2314. current_op.chain_forward=false;
  2315. }
  2316. void TextEdit::set_draw_tabs(bool p_draw) {
  2317. draw_tabs=p_draw;
  2318. }
  2319. bool TextEdit::is_drawing_tabs() const{
  2320. return draw_tabs;
  2321. }
  2322. uint32_t TextEdit::get_version() const {
  2323. return current_op.version;
  2324. }
  2325. uint32_t TextEdit::get_saved_version() const {
  2326. return saved_version;
  2327. }
  2328. void TextEdit::tag_saved_version() {
  2329. saved_version=get_version();
  2330. }
  2331. int TextEdit::get_v_scroll() const {
  2332. return v_scroll->get_val();
  2333. }
  2334. void TextEdit::set_v_scroll(int p_scroll) {
  2335. v_scroll->set_val(p_scroll);
  2336. cursor.line_ofs=p_scroll;
  2337. }
  2338. int TextEdit::get_h_scroll() const {
  2339. return h_scroll->get_val();
  2340. }
  2341. void TextEdit::set_h_scroll(int p_scroll) {
  2342. h_scroll->set_val(p_scroll);
  2343. }
  2344. void TextEdit::set_completion(bool p_enabled,const Vector<String>& p_prefixes) {
  2345. completion_prefixes.clear();
  2346. completion_enabled=p_enabled;
  2347. for(int i=0;i<p_prefixes.size();i++)
  2348. completion_prefixes.insert(p_prefixes[i]);
  2349. }
  2350. void TextEdit::_confirm_completion() {
  2351. String remaining=completion_current.substr(completion_base.length(),completion_current.length()-completion_base.length());
  2352. String l = text[cursor.line];
  2353. bool same=true;
  2354. //if what is going to be inserted is the same as what it is, don't change it
  2355. for(int i=0;i<remaining.length();i++) {
  2356. int c=i+cursor.column;
  2357. if (c>=l.length() || l[c]!=remaining[i]) {
  2358. same=false;
  2359. break;
  2360. }
  2361. }
  2362. if (same)
  2363. cursor_set_column(cursor.column+remaining.length());
  2364. else {
  2365. insert_text_at_cursor(remaining);
  2366. if (remaining.ends_with("(") && auto_brace_completion_enabled) {
  2367. insert_text_at_cursor(")");
  2368. cursor.column--;
  2369. }
  2370. }
  2371. _cancel_completion();
  2372. }
  2373. void TextEdit::_cancel_code_hint() {
  2374. completion_hint="";
  2375. update();
  2376. }
  2377. void TextEdit::_cancel_completion() {
  2378. if (!completion_active)
  2379. return;
  2380. completion_active=false;
  2381. update();
  2382. }
  2383. static bool _is_completable(CharType c) {
  2384. return !_is_symbol(c) || c=='"' || c=='\'';
  2385. }
  2386. void TextEdit::_update_completion_candidates() {
  2387. String l = text[cursor.line];
  2388. int cofs = CLAMP(cursor.column,0,l.length());
  2389. String s;
  2390. //look for keywords first
  2391. bool inquote=false;
  2392. int first_quote=-1;
  2393. int c=cofs-1;
  2394. while(c>=0) {
  2395. if (l[c]=='"' || l[c]=='\'') {
  2396. inquote=!inquote;
  2397. if (first_quote==-1)
  2398. first_quote=c;
  2399. }
  2400. c--;
  2401. }
  2402. bool pre_keyword=false;
  2403. bool cancel=false;
  2404. //print_line("inquote: "+itos(inquote)+"first quote "+itos(first_quote)+" cofs-1 "+itos(cofs-1));
  2405. if (!inquote && first_quote==cofs-1) {
  2406. //no completion here
  2407. //print_line("cancel!");
  2408. cancel=true;
  2409. } if (inquote && first_quote!=-1) {
  2410. s=l.substr(first_quote,cofs-first_quote);
  2411. //print_line("s: 1"+s);
  2412. } else if (cofs>0 && l[cofs-1]==' ') {
  2413. int kofs=cofs-1;
  2414. String kw;
  2415. while (kofs>=0 && l[kofs]==' ')
  2416. kofs--;
  2417. while(kofs>=0 && l[kofs]>32 && _is_completable(l[kofs])) {
  2418. kw=String::chr(l[kofs])+kw;
  2419. kofs--;
  2420. }
  2421. pre_keyword=keywords.has(kw);
  2422. //print_line("KW "+kw+"? "+itos(pre_keyword));
  2423. } else {
  2424. while(cofs>0 && l[cofs-1]>32 && _is_completable(l[cofs-1])) {
  2425. s=String::chr(l[cofs-1])+s;
  2426. if (l[cofs-1]=='\'' || l[cofs-1]=='"')
  2427. break;
  2428. cofs--;
  2429. }
  2430. }
  2431. update();
  2432. if (cancel || (!pre_keyword && s=="" && (cofs==0 || !completion_prefixes.has(String::chr(l[cofs-1]))))) {
  2433. //none to complete, cancel
  2434. _cancel_completion();
  2435. return;
  2436. }
  2437. completion_options.clear();
  2438. completion_index=0;
  2439. completion_base=s;
  2440. int ci_match=0;
  2441. for(int i=0;i<completion_strings.size();i++) {
  2442. if (completion_strings[i].begins_with(s)) {
  2443. completion_options.push_back(completion_strings[i]);
  2444. int m=0;
  2445. int max=MIN(completion_current.length(),completion_strings[i].length());
  2446. if (max<ci_match)
  2447. continue;
  2448. for(int j=0;j<max;j++) {
  2449. if (j>=completion_strings[i].length())
  2450. break;
  2451. if (completion_current[j]!=completion_strings[i][j])
  2452. break;
  2453. m++;
  2454. }
  2455. if (m>ci_match) {
  2456. ci_match=m;
  2457. completion_index=completion_options.size()-1;
  2458. }
  2459. }
  2460. }
  2461. if (completion_options.size()==0) {
  2462. //no options to complete, cancel
  2463. _cancel_completion();
  2464. return;
  2465. }
  2466. completion_current=completion_options[completion_index];
  2467. #if 0 // even there's only one option, user still get the chance to choose using it or not
  2468. if (completion_options.size()==1) {
  2469. //one option to complete, just complete it automagically
  2470. _confirm_completion();
  2471. // insert_text_at_cursor(completion_options[0].substr(s.length(),completion_options[0].length()-s.length()));
  2472. _cancel_completion();
  2473. return;
  2474. }
  2475. #endif
  2476. if (completion_options.size()==1 && s==completion_options[0])
  2477. _cancel_completion();
  2478. completion_enabled=true;
  2479. }
  2480. void TextEdit::query_code_comple() {
  2481. String l = text[cursor.line];
  2482. int ofs = CLAMP(cursor.column,0,l.length());
  2483. bool inquote=false;
  2484. int c=ofs-1;
  2485. while(c>=0) {
  2486. if (l[c]=='"' || l[c]=='\'')
  2487. inquote=!inquote;
  2488. c--;
  2489. }
  2490. if (ofs>0 && (inquote || _is_completable(l[ofs-1]) || completion_prefixes.has(String::chr(l[ofs-1]))))
  2491. emit_signal("request_completion");
  2492. }
  2493. void TextEdit::set_code_hint(const String& p_hint) {
  2494. completion_hint=p_hint;
  2495. completion_hint_offset=-0xFFFF;
  2496. update();
  2497. }
  2498. void TextEdit::code_complete(const Vector<String> &p_strings) {
  2499. completion_strings=p_strings;
  2500. completion_active=true;
  2501. completion_current="";
  2502. completion_index=0;
  2503. _update_completion_candidates();
  2504. //
  2505. }
  2506. String TextEdit::get_tooltip(const Point2& p_pos) const {
  2507. if (!tooltip_obj)
  2508. return Control::get_tooltip(p_pos);
  2509. int row,col;
  2510. if (!_get_mouse_pos(p_pos, row,col)) {
  2511. return Control::get_tooltip(p_pos);
  2512. }
  2513. String s = text[row];
  2514. if (s.length()==0)
  2515. return Control::get_tooltip(p_pos);
  2516. int beg=CLAMP(col,0,s.length());
  2517. int end=beg;
  2518. if (s[beg]>32 || beg==s.length()) {
  2519. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  2520. while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) {
  2521. beg--;
  2522. }
  2523. while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) {
  2524. end++;
  2525. }
  2526. if (end<s.length())
  2527. end+=1;
  2528. String tt = tooltip_obj->call(tooltip_func,s.substr(beg,end-beg),tooltip_ud);
  2529. return tt;
  2530. }
  2531. return Control::get_tooltip(p_pos);
  2532. }
  2533. void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName& p_function,const Variant& p_udata) {
  2534. tooltip_obj=p_obj;
  2535. tooltip_func=p_function;
  2536. tooltip_ud=p_udata;
  2537. }
  2538. void TextEdit::set_line(int line, String new_text)
  2539. {
  2540. if (line < 0 || line > text.size())
  2541. return;
  2542. _remove_text(line, 0, line, text[line].length());
  2543. _insert_text(line, 0, new_text);
  2544. if (cursor.line==line) {
  2545. cursor.column=MIN(cursor.column,new_text.length());
  2546. }
  2547. }
  2548. void TextEdit::insert_at(const String &p_text, int at)
  2549. {
  2550. cursor_set_column(0);
  2551. cursor_set_line(at);
  2552. _insert_text(at, 0, p_text+"\n");
  2553. }
  2554. void TextEdit::set_show_line_numbers(bool p_show) {
  2555. line_numbers=p_show;
  2556. update();
  2557. }
  2558. void TextEdit::_bind_methods() {
  2559. ObjectTypeDB::bind_method(_MD("_input_event"),&TextEdit::_input_event);
  2560. ObjectTypeDB::bind_method(_MD("_scroll_moved"),&TextEdit::_scroll_moved);
  2561. ObjectTypeDB::bind_method(_MD("_cursor_changed_emit"),&TextEdit::_cursor_changed_emit);
  2562. ObjectTypeDB::bind_method(_MD("_text_changed_emit"),&TextEdit::_text_changed_emit);
  2563. ObjectTypeDB::bind_method(_MD("_push_current_op"),&TextEdit::_push_current_op);
  2564. BIND_CONSTANT( SEARCH_MATCH_CASE );
  2565. BIND_CONSTANT( SEARCH_WHOLE_WORDS );
  2566. BIND_CONSTANT( SEARCH_BACKWARDS );
  2567. /*
  2568. ObjectTypeDB::bind_method(_MD("delete_char"),&TextEdit::delete_char);
  2569. ObjectTypeDB::bind_method(_MD("delete_line"),&TextEdit::delete_line);
  2570. */
  2571. ObjectTypeDB::bind_method(_MD("set_text","text"),&TextEdit::set_text);
  2572. ObjectTypeDB::bind_method(_MD("insert_text_at_cursor","text"),&TextEdit::insert_text_at_cursor);
  2573. ObjectTypeDB::bind_method(_MD("get_line_count"),&TextEdit::get_line_count);
  2574. ObjectTypeDB::bind_method(_MD("get_text"),&TextEdit::get_text);
  2575. ObjectTypeDB::bind_method(_MD("get_line"),&TextEdit::get_line);
  2576. ObjectTypeDB::bind_method(_MD("cursor_set_column","column"),&TextEdit::cursor_set_column);
  2577. ObjectTypeDB::bind_method(_MD("cursor_set_line","line"),&TextEdit::cursor_set_line);
  2578. ObjectTypeDB::bind_method(_MD("cursor_get_column"),&TextEdit::cursor_get_column);
  2579. ObjectTypeDB::bind_method(_MD("cursor_get_line"),&TextEdit::cursor_get_line);
  2580. ObjectTypeDB::bind_method(_MD("set_readonly","enable"),&TextEdit::set_readonly);
  2581. ObjectTypeDB::bind_method(_MD("set_wrap","enable"),&TextEdit::set_wrap);
  2582. ObjectTypeDB::bind_method(_MD("set_max_chars","amount"),&TextEdit::set_max_chars);
  2583. ObjectTypeDB::bind_method(_MD("cut"),&TextEdit::cut);
  2584. ObjectTypeDB::bind_method(_MD("copy"),&TextEdit::copy);
  2585. ObjectTypeDB::bind_method(_MD("paste"),&TextEdit::paste);
  2586. ObjectTypeDB::bind_method(_MD("select_all"),&TextEdit::select_all);
  2587. ObjectTypeDB::bind_method(_MD("select","from_line","from_column","to_line","to_column"),&TextEdit::select);
  2588. ObjectTypeDB::bind_method(_MD("is_selection_active"),&TextEdit::is_selection_active);
  2589. ObjectTypeDB::bind_method(_MD("get_selection_from_line"),&TextEdit::get_selection_from_line);
  2590. ObjectTypeDB::bind_method(_MD("get_selection_from_column"),&TextEdit::get_selection_from_column);
  2591. ObjectTypeDB::bind_method(_MD("get_selection_to_line"),&TextEdit::get_selection_to_line);
  2592. ObjectTypeDB::bind_method(_MD("get_selection_to_column"),&TextEdit::get_selection_to_column);
  2593. ObjectTypeDB::bind_method(_MD("get_selection_text"),&TextEdit::get_selection_text);
  2594. ObjectTypeDB::bind_method(_MD("get_word_under_cursor"),&TextEdit::get_word_under_cursor);
  2595. ObjectTypeDB::bind_method(_MD("search","flags","from_line","from_column","to_line","to_column"),&TextEdit::_search_bind);
  2596. ObjectTypeDB::bind_method(_MD("undo"),&TextEdit::undo);
  2597. ObjectTypeDB::bind_method(_MD("redo"),&TextEdit::redo);
  2598. ObjectTypeDB::bind_method(_MD("clear_undo_history"),&TextEdit::clear_undo_history);
  2599. ObjectTypeDB::bind_method(_MD("set_syntax_coloring","enable"),&TextEdit::set_syntax_coloring);
  2600. ObjectTypeDB::bind_method(_MD("is_syntax_coloring_enabled"),&TextEdit::is_syntax_coloring_enabled);
  2601. ObjectTypeDB::bind_method(_MD("add_keyword_color","keyword","color"),&TextEdit::add_keyword_color);
  2602. ObjectTypeDB::bind_method(_MD("add_color_region","begin_key","end_key","color","line_only"),&TextEdit::add_color_region,DEFVAL(false));
  2603. ObjectTypeDB::bind_method(_MD("set_symbol_color","color"),&TextEdit::set_symbol_color);
  2604. ObjectTypeDB::bind_method(_MD("set_custom_bg_color","color"),&TextEdit::set_custom_bg_color);
  2605. ObjectTypeDB::bind_method(_MD("clear_colors"),&TextEdit::clear_colors);
  2606. ADD_SIGNAL(MethodInfo("cursor_changed"));
  2607. ADD_SIGNAL(MethodInfo("text_changed"));
  2608. ADD_SIGNAL(MethodInfo("request_completion"));
  2609. }
  2610. TextEdit::TextEdit() {
  2611. readonly=false;
  2612. setting_row=false;
  2613. draw_tabs=false;
  2614. max_chars=0;
  2615. clear();
  2616. wrap=false;
  2617. set_focus_mode(FOCUS_ALL);
  2618. _update_caches();
  2619. cache.size=Size2(1,1);
  2620. cache.row_height=1;
  2621. cache.line_spacing=1;
  2622. cache.line_number_w=1;
  2623. tab_size=4;
  2624. text.set_tab_size(tab_size);
  2625. text.clear();
  2626. // text.insert(1,"Mongolia..");
  2627. // text.insert(2,"PAIS GENEROSO!!");
  2628. text.set_color_regions(&color_regions);
  2629. h_scroll = memnew( HScrollBar );
  2630. v_scroll = memnew( VScrollBar );
  2631. add_child(h_scroll);
  2632. add_child(v_scroll);
  2633. updating_scrolls=false;
  2634. selection.active=false;
  2635. h_scroll->connect("value_changed", this,"_scroll_moved");
  2636. v_scroll->connect("value_changed", this,"_scroll_moved");
  2637. cursor_changed_dirty=false;
  2638. text_changed_dirty=false;
  2639. selection.selecting_mode=Selection::MODE_NONE;
  2640. selection.selecting_line=0;
  2641. selection.selecting_column=0;
  2642. selection.selecting_text=false;
  2643. selection.active=false;
  2644. syntax_coloring=false;
  2645. custom_bg_color=Color(0,0,0,0);
  2646. idle_detect = memnew( Timer );
  2647. add_child(idle_detect);
  2648. idle_detect->set_one_shot(true);
  2649. idle_detect->set_wait_time(GLOBAL_DEF("display/text_edit_idle_detect_sec",3));
  2650. idle_detect->connect("timeout", this,"_push_current_op");
  2651. #if 0
  2652. syntax_coloring=true;
  2653. keywords["void"]=Color(0.3,0.0,0.1);
  2654. keywords["int"]=Color(0.3,0.0,0.1);
  2655. keywords["function"]=Color(0.3,0.0,0.1);
  2656. keywords["class"]=Color(0.3,0.0,0.1);
  2657. keywords["extends"]=Color(0.3,0.0,0.1);
  2658. keywords["constructor"]=Color(0.3,0.0,0.1);
  2659. symbol_color=Color(0.1,0.0,0.3,1.0);
  2660. color_regions.push_back(ColorRegion("/*","*/",Color(0.4,0.6,0,4)));
  2661. color_regions.push_back(ColorRegion("//","",Color(0.6,0.6,0.4)));
  2662. color_regions.push_back(ColorRegion("\"","\"",Color(0.4,0.7,0.7)));
  2663. color_regions.push_back(ColorRegion("'","'",Color(0.4,0.8,0.8)));
  2664. color_regions.push_back(ColorRegion("#","",Color(0.2,1.0,0.2)));
  2665. #endif
  2666. current_op.type=TextOperation::TYPE_NONE;
  2667. undo_enabled=true;
  2668. undo_stack_pos=NULL;
  2669. setting_text=false;
  2670. last_dblclk=0;
  2671. current_op.version=0;
  2672. version=0;
  2673. saved_version=0;
  2674. completion_enabled=false;
  2675. completion_active=false;
  2676. completion_line_ofs=0;
  2677. tooltip_obj=NULL;
  2678. line_numbers=false;
  2679. next_operation_is_complex=false;
  2680. auto_brace_completion_enabled=false;
  2681. brace_matching_enabled=false;
  2682. }
  2683. TextEdit::~TextEdit()
  2684. {
  2685. }