text_edit.cpp 91 KB

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