text_edit.cpp 115 KB

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