text_edit.cpp 76 KB

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