text_edit.cpp 86 KB

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