text_edit.cpp 91 KB

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