2
0

text_edit.cpp 72 KB

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