text_edit.cpp 87 KB

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