2
0

text_edit.cpp 96 KB

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