text_edit.cpp 105 KB

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