text_edit.cpp 103 KB

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