text_edit.cpp 130 KB

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