text_edit.cpp 102 KB

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