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