text_edit.cpp 86 KB

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