text_edit.cpp 96 KB

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