text_edit.cpp 130 KB

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