text_edit.cpp 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553
  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() * cache.line_spacing;
  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. int scrollw = get_constant("completion_scroll_width");
  555. Color scrollc = get_color("completion_scroll_color");
  556. int lines = MIN(completion_options.size(),maxlines);
  557. int w=0;
  558. int h=lines*get_row_height();
  559. int nofs = cache.font->get_string_size(completion_base).width;
  560. if (completion_options.size() < 50) {
  561. for(int i=0;i<completion_options.size();i++) {
  562. int w2=MIN(cache.font->get_string_size(completion_options[i]).x,cmax_width);
  563. if (w2>w)
  564. w=w2;
  565. }
  566. } else {
  567. w=cmax_width;
  568. }
  569. int th = h + csb->get_minimum_size().y;
  570. if (cursor_pos.y+get_row_height()+th > get_size().height) {
  571. completion_rect.pos.y=cursor_pos.y-th;
  572. } else {
  573. completion_rect.pos.y=cursor_pos.y+get_row_height()+csb->get_offset().y;
  574. }
  575. if (cursor_pos.x-nofs+w+scrollw > get_size().width) {
  576. completion_rect.pos.x=get_size().width-w-scrollw;
  577. } else {
  578. completion_rect.pos.x=cursor_pos.x-nofs;
  579. }
  580. completion_rect.size.width=w;
  581. completion_rect.size.height=h;
  582. if (completion_options.size()<=maxlines)
  583. scrollw=0;
  584. draw_style_box(csb,Rect2(completion_rect.pos-csb->get_offset(),completion_rect.size+csb->get_minimum_size()+Size2(scrollw,0)));
  585. int line_from = CLAMP(completion_index - lines/2, 0, completion_options.size() - lines);
  586. 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())));
  587. draw_rect(Rect2(completion_rect.pos,Size2(nofs,completion_rect.size.height)),existing);
  588. for(int i=0;i<lines;i++) {
  589. int l = line_from + i;
  590. ERR_CONTINUE( l < 0 || l>= completion_options.size());
  591. draw_string(cache.font,Point2(completion_rect.pos.x,completion_rect.pos.y+i*get_row_height()+cache.font->get_ascent()),completion_options[l],cache.font_color,completion_rect.size.width);
  592. }
  593. if (scrollw) {
  594. //draw a small scroll rectangle to show a position in the options
  595. float r = maxlines / (float)completion_options.size();
  596. float o = line_from / (float)completion_options.size();
  597. 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);
  598. }
  599. completion_line_ofs=line_from;
  600. }
  601. if (completion_hint!="") {
  602. Ref<StyleBox> sb = get_stylebox("panel","TooltipPanel");
  603. Ref<Font> font = cache.font;
  604. Color font_color = get_color("font_color","TooltipLabel");
  605. int max_w=0;
  606. int sc = completion_hint.get_slice_count("\n");
  607. int offset=0;
  608. int spacing=0;
  609. for(int i=0;i<sc;i++) {
  610. String l = completion_hint.get_slice("\n",i);
  611. int len = font->get_string_size(l).x;
  612. max_w = MAX(len,max_w);
  613. if (i==0) {
  614. offset = font->get_string_size(l.substr(0,l.find(String::chr(0xFFFF)))).x;
  615. } else {
  616. spacing+=cache.line_spacing;
  617. }
  618. }
  619. Size2 size = Size2(max_w,sc*font->get_height()+spacing);
  620. Size2 minsize = size+sb->get_minimum_size();
  621. if (completion_hint_offset==-0xFFFF) {
  622. completion_hint_offset=cursor_pos.x-offset;
  623. }
  624. Point2 hint_ofs = Vector2(completion_hint_offset,cursor_pos.y-minsize.y);
  625. draw_style_box(sb,Rect2(hint_ofs,minsize));
  626. spacing=0;
  627. for(int i=0;i<sc;i++) {
  628. int begin=0;
  629. int end=0;
  630. String l = completion_hint.get_slice("\n",i);
  631. if (l.find(String::chr(0xFFFF))!=-1) {
  632. begin = font->get_string_size(l.substr(0,l.find(String::chr(0xFFFF)))).x;
  633. end = font->get_string_size(l.substr(0,l.rfind(String::chr(0xFFFF)))).x;
  634. }
  635. 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);
  636. if (end>0) {
  637. Vector2 b = hint_ofs+sb->get_offset()+Vector2(begin,font->get_height()+font->get_height()*i+spacing-1);
  638. draw_line(b,b+Vector2(end-begin,0),font_color);
  639. }
  640. spacing+=cache.line_spacing;
  641. }
  642. }
  643. } break;
  644. case NOTIFICATION_FOCUS_ENTER: {
  645. if (OS::get_singleton()->has_virtual_keyboard())
  646. OS::get_singleton()->show_virtual_keyboard(get_text(),get_global_rect());
  647. } break;
  648. case NOTIFICATION_FOCUS_EXIT: {
  649. if (OS::get_singleton()->has_virtual_keyboard())
  650. OS::get_singleton()->hide_virtual_keyboard();
  651. } break;
  652. }
  653. }
  654. void TextEdit::_consume_pair_symbol(CharType ch) {
  655. int cursor_position_to_move = cursor_get_column() + 1;
  656. CharType ch_single[2] = {ch, 0};
  657. CharType ch_single_pair[2] = {_get_right_pair_symbol(ch), 0};
  658. CharType ch_pair[3] = {ch, _get_right_pair_symbol(ch), 0};
  659. if(is_selection_active()) {
  660. int new_column,new_line;
  661. _begin_compex_operation();
  662. _insert_text(get_selection_from_line(), get_selection_from_column(),
  663. ch_single,
  664. &new_line, &new_column);
  665. int to_col_offset = 0;
  666. if(get_selection_from_line() == get_selection_to_line())
  667. to_col_offset = 1;
  668. _insert_text(get_selection_to_line(),
  669. get_selection_to_column() + to_col_offset,
  670. ch_single_pair,
  671. &new_line,&new_column);
  672. _end_compex_operation();
  673. cursor_set_line(get_selection_to_line());
  674. cursor_set_column(get_selection_to_column() + to_col_offset);
  675. deselect();
  676. update();
  677. return;
  678. }
  679. if( (ch == '\'' || ch == '"') &&
  680. cursor_get_column() > 0 &&
  681. _is_text_char(text[cursor.line][cursor_get_column() - 1])
  682. ) {
  683. insert_text_at_cursor(ch_single);
  684. cursor_set_column(cursor_position_to_move);
  685. return;
  686. }
  687. if(cursor_get_column() < text[cursor.line].length()) {
  688. if(_is_text_char(text[cursor.line][cursor_get_column()])) {
  689. insert_text_at_cursor(ch_single);
  690. cursor_set_column(cursor_position_to_move);
  691. return;
  692. }
  693. if( _is_pair_right_symbol(ch) &&
  694. text[cursor.line][cursor_get_column()] == ch
  695. ) {
  696. cursor_set_column(cursor_position_to_move);
  697. return;
  698. }
  699. }
  700. insert_text_at_cursor(ch_pair);
  701. cursor_set_column(cursor_position_to_move);
  702. return;
  703. }
  704. void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
  705. bool remove_right_symbol = false;
  706. if(cursor.column < text[cursor.line].length() && cursor.column > 0) {
  707. CharType left_char = text[cursor.line][cursor.column - 1];
  708. CharType right_char = text[cursor.line][cursor.column];
  709. if(right_char == _get_right_pair_symbol(left_char)) {
  710. remove_right_symbol = true;
  711. }
  712. }
  713. if(remove_right_symbol) {
  714. _remove_text(prev_line,prev_column,cursor.line,cursor.column + 1);
  715. } else {
  716. _remove_text(prev_line,prev_column,cursor.line,cursor.column);
  717. }
  718. }
  719. void TextEdit::backspace_at_cursor() {
  720. if (cursor.column==0 && cursor.line==0)
  721. return;
  722. int prev_line = cursor.column?cursor.line:cursor.line-1;
  723. int prev_column = cursor.column?(cursor.column-1):(text[cursor.line-1].length());
  724. if(auto_brace_completion_enabled &&
  725. cursor.column > 0 &&
  726. _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
  727. _consume_backspace_for_pair_symbol(prev_line, prev_column);
  728. } else {
  729. _remove_text(prev_line,prev_column,cursor.line,cursor.column);
  730. }
  731. cursor_set_line(prev_line);
  732. cursor_set_column(prev_column);
  733. }
  734. bool TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
  735. int row=p_mouse.y;
  736. row-=cache.style_normal->get_margin(MARGIN_TOP);
  737. row/=get_row_height();
  738. if (row<0 || row>=get_visible_rows())
  739. return false;
  740. row+=cursor.line_ofs;
  741. int col=0;
  742. if (row>=text.size()) {
  743. row=text.size()-1;
  744. col=text[row].size();
  745. } else {
  746. col=p_mouse.x-(cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w);
  747. col+=cursor.x_ofs;
  748. col=get_char_pos_for( col, get_line(row) );
  749. }
  750. r_row=row;
  751. r_col=col;
  752. return true;
  753. }
  754. void TextEdit::_input_event(const InputEvent& p_input_event) {
  755. switch(p_input_event.type) {
  756. case InputEvent::MOUSE_BUTTON: {
  757. const InputEventMouseButton &mb=p_input_event.mouse_button;
  758. if (completion_active && completion_rect.has_point(Point2(mb.x,mb.y))) {
  759. if (!mb.pressed)
  760. return;
  761. if (mb.button_index==BUTTON_WHEEL_UP) {
  762. if (completion_index>0) {
  763. completion_index--;
  764. completion_current=completion_options[completion_index];
  765. update();
  766. }
  767. }
  768. if (mb.button_index==BUTTON_WHEEL_DOWN) {
  769. if (completion_index<completion_options.size()-1) {
  770. completion_index++;
  771. completion_current=completion_options[completion_index];
  772. update();
  773. }
  774. }
  775. if (mb.button_index==BUTTON_LEFT) {
  776. completion_index=CLAMP(completion_line_ofs+(mb.y-completion_rect.pos.y)/get_row_height(),0,completion_options.size()-1);
  777. completion_current=completion_options[completion_index];
  778. update();
  779. if (mb.doubleclick)
  780. _confirm_completion();
  781. }
  782. return;
  783. } else {
  784. _cancel_completion();
  785. _cancel_code_hint();
  786. }
  787. if (mb.pressed) {
  788. if (mb.button_index==BUTTON_WHEEL_UP) {
  789. v_scroll->set_val( v_scroll->get_val() -3 );
  790. }
  791. if (mb.button_index==BUTTON_WHEEL_DOWN) {
  792. v_scroll->set_val( v_scroll->get_val() +3 );
  793. }
  794. if (mb.button_index==BUTTON_LEFT) {
  795. int row,col;
  796. if (!_get_mouse_pos(Point2i(mb.x,mb.y), row,col))
  797. return;
  798. int prev_col=cursor.column;
  799. int prev_line=cursor.line;
  800. cursor_set_line( row );
  801. cursor_set_column( col );
  802. if (mb.mod.shift && (cursor.column!=prev_col || cursor.line!=prev_line)) {
  803. selection.active=true;
  804. selection.selecting_mode=Selection::MODE_POINTER;
  805. selection.from_column=prev_col;
  806. selection.from_line=prev_line;
  807. selection.to_column=cursor.column;
  808. selection.to_line=cursor.line;
  809. if (selection.from_column>selection.to_column) {
  810. SWAP(selection.from_column,selection.to_column);
  811. SWAP(selection.from_line,selection.to_line);
  812. }
  813. selection.selecting_line=prev_line;
  814. selection.selecting_column=prev_col;
  815. update();
  816. } else {
  817. //if sel active and dblick last time < something
  818. //else
  819. selection.active=false;
  820. selection.selecting_mode=Selection::MODE_POINTER;
  821. selection.selecting_line=row;
  822. selection.selecting_column=col;
  823. }
  824. if (!mb.doubleclick && (OS::get_singleton()->get_ticks_msec()-last_dblclk)<600) {
  825. //tripleclick select line
  826. select(cursor.line,0,cursor.line,text[cursor.line].length());
  827. last_dblclk=0;
  828. } else if (mb.doubleclick && text[cursor.line].length()) {
  829. //doubleclick select world
  830. String s = text[cursor.line];
  831. int beg=CLAMP(cursor.column,0,s.length());
  832. int end=beg;
  833. if (s[beg]>32 || beg==s.length()) {
  834. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  835. while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) {
  836. beg--;
  837. }
  838. while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) {
  839. end++;
  840. }
  841. if (end<s.length())
  842. end+=1;
  843. select(cursor.line,beg,cursor.line,end);
  844. }
  845. last_dblclk = OS::get_singleton()->get_ticks_msec();
  846. }
  847. update();
  848. }
  849. } else {
  850. selection.selecting_mode=Selection::MODE_NONE;
  851. // notify to show soft keyboard
  852. notification(NOTIFICATION_FOCUS_ENTER);
  853. }
  854. } break;
  855. case InputEvent::MOUSE_MOTION: {
  856. const InputEventMouseMotion &mm=p_input_event.mouse_motion;
  857. if (mm.button_mask&BUTTON_MASK_LEFT) {
  858. int row,col;
  859. if (!_get_mouse_pos(Point2i(mm.x,mm.y), row,col))
  860. return;
  861. if (selection.selecting_mode==Selection::MODE_POINTER) {
  862. select(selection.selecting_line,selection.selecting_column,row,col);
  863. cursor_set_line( row );
  864. cursor_set_column( col );
  865. update();
  866. }
  867. }
  868. } break;
  869. case InputEvent::KEY: {
  870. InputEventKey k=p_input_event.key;
  871. if (!k.pressed)
  872. return;
  873. if (completion_active) {
  874. bool valid=true;
  875. if (k.mod.command || k.mod.alt || k.mod.meta)
  876. valid=false;
  877. if (valid) {
  878. if (k.scancode==KEY_UP) {
  879. if (completion_index>0) {
  880. completion_index--;
  881. completion_current=completion_options[completion_index];
  882. update();
  883. }
  884. accept_event();
  885. return;
  886. }
  887. if (k.scancode==KEY_DOWN) {
  888. if (completion_index<completion_options.size()-1) {
  889. completion_index++;
  890. completion_current=completion_options[completion_index];
  891. update();
  892. }
  893. accept_event();
  894. return;
  895. }
  896. if (k.scancode==KEY_PAGEUP) {
  897. completion_index-=get_constant("completion_lines");
  898. if (completion_index<0)
  899. completion_index=0;
  900. completion_current=completion_options[completion_index];
  901. update();
  902. accept_event();
  903. return;
  904. }
  905. if (k.scancode==KEY_PAGEDOWN) {
  906. completion_index+=get_constant("completion_lines");
  907. if (completion_index>=completion_options.size())
  908. completion_index=completion_options.size()-1;
  909. completion_current=completion_options[completion_index];
  910. update();
  911. accept_event();
  912. return;
  913. }
  914. if (k.scancode==KEY_HOME) {
  915. completion_index=0;
  916. completion_current=completion_options[completion_index];
  917. update();
  918. accept_event();
  919. return;
  920. }
  921. if (k.scancode==KEY_END) {
  922. completion_index=completion_options.size()-1;
  923. completion_current=completion_options[completion_index];
  924. update();
  925. accept_event();
  926. return;
  927. }
  928. if (k.scancode==KEY_DOWN) {
  929. if (completion_index<completion_options.size()-1) {
  930. completion_index++;
  931. completion_current=completion_options[completion_index];
  932. update();
  933. }
  934. accept_event();
  935. return;
  936. }
  937. if (k.scancode==KEY_RETURN || k.scancode==KEY_TAB) {
  938. _confirm_completion();
  939. accept_event();
  940. return;
  941. }
  942. if (k.scancode==KEY_BACKSPACE) {
  943. backspace_at_cursor();
  944. _update_completion_candidates();
  945. accept_event();
  946. return;
  947. }
  948. if (k.scancode==KEY_SHIFT) {
  949. accept_event();
  950. return;
  951. }
  952. if (k.unicode>32) {
  953. if (cursor.column<text[cursor.line].length() && text[cursor.line][cursor.column]==k.unicode) {
  954. //same char, move ahead
  955. cursor_set_column(cursor.column+1);
  956. } else {
  957. //different char, go back
  958. const CharType chr[2] = {k.unicode, 0};
  959. if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  960. _consume_pair_symbol(chr[0]);
  961. } else {
  962. _insert_text_at_cursor(chr);
  963. }
  964. }
  965. _update_completion_candidates();
  966. accept_event();
  967. return;
  968. }
  969. }
  970. _cancel_completion();
  971. }
  972. /* TEST CONTROL FIRST!! */
  973. // some remaps for duplicate functions..
  974. if (k.mod.command && !k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode==KEY_INSERT) {
  975. k.scancode=KEY_C;
  976. }
  977. if (!k.mod.command && k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode==KEY_INSERT) {
  978. k.scancode=KEY_V;
  979. k.mod.command=true;
  980. k.mod.shift=false;
  981. }
  982. // stuff to do when selection is active..
  983. if (selection.active) {
  984. bool clear=false;
  985. bool unselect=false;
  986. bool dobreak=false;
  987. switch(k.scancode) {
  988. case KEY_TAB: {
  989. String txt = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  990. String prev_txt=txt;
  991. if (k.mod.shift) {
  992. for(int i=0;i<txt.length();i++) {
  993. if (((i>0 && txt[i-1]=='\n') || (i==0 /*&& selection.from_column==0*/)) && (txt[i]=='\t' || txt[i]==' ')) {
  994. txt.remove(i);
  995. //i--;
  996. }
  997. }
  998. } else {
  999. for(int i=0;i<txt.length();i++) {
  1000. if (((i>0 && txt[i-1]=='\n') || (i==0 /*&& selection.from_column==0*/))) {
  1001. txt=txt.insert(i,"\t");
  1002. //i--;
  1003. }
  1004. }
  1005. }
  1006. if (txt!=prev_txt) {
  1007. int sel_line=selection.from_line;
  1008. int sel_column=selection.from_column;
  1009. cursor_set_line(selection.from_line);
  1010. cursor_set_column(selection.from_column);
  1011. _begin_compex_operation();
  1012. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1013. _insert_text_at_cursor(txt);
  1014. _end_compex_operation();
  1015. selection.active=true;
  1016. selection.from_column=sel_column;
  1017. selection.from_line=sel_line;
  1018. selection.to_column=cursor.column;
  1019. selection.to_line=cursor.line;
  1020. update();
  1021. }
  1022. dobreak=true;
  1023. accept_event();
  1024. } break;
  1025. case KEY_X:
  1026. case KEY_C:
  1027. //special keys often used with control, wait...
  1028. clear=(!k.mod.command || k.mod.shift || k.mod.alt );
  1029. break;
  1030. case KEY_DELETE:
  1031. case KEY_BACKSPACE:
  1032. accept_event();
  1033. clear=true; dobreak=true;
  1034. break;
  1035. case KEY_LEFT:
  1036. case KEY_RIGHT:
  1037. case KEY_UP:
  1038. case KEY_DOWN:
  1039. case KEY_PAGEUP:
  1040. case KEY_PAGEDOWN:
  1041. case KEY_HOME:
  1042. case KEY_END:
  1043. // ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys)
  1044. if (k.mod.command || k.mod.shift || k.mod.alt || k.mod.command)
  1045. break;
  1046. unselect=true;
  1047. break;
  1048. default:
  1049. if (k.unicode>=32 && !k.mod.command && !k.mod.alt && !k.mod.meta)
  1050. clear=true;
  1051. if (auto_brace_completion_enabled && _is_pair_left_symbol(k.unicode))
  1052. clear=false;
  1053. }
  1054. if (unselect) {
  1055. selection.active=false;
  1056. selection.selecting_mode=Selection::MODE_NONE;
  1057. update();
  1058. }
  1059. if (clear) {
  1060. selection.active=false;
  1061. update();
  1062. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1063. cursor_set_line(selection.from_line);
  1064. cursor_set_column(selection.from_column);
  1065. update();
  1066. }
  1067. if (dobreak)
  1068. break;
  1069. }
  1070. selection.selecting_test=false;
  1071. bool scancode_handled=true;
  1072. // special scancode test...
  1073. switch (k.scancode) {
  1074. case KEY_ENTER:
  1075. case KEY_RETURN: {
  1076. String ins="\n";
  1077. //keep indentation
  1078. for(int i=0;i<text[cursor.line].length();i++) {
  1079. if (text[cursor.line][i]=='\t')
  1080. ins+="\t";
  1081. else
  1082. break;
  1083. }
  1084. _insert_text_at_cursor(ins);
  1085. _push_current_op();
  1086. } break;
  1087. case KEY_ESCAPE: {
  1088. if (completion_hint!="") {
  1089. completion_hint="";
  1090. update();
  1091. }
  1092. } break;
  1093. case KEY_TAB: {
  1094. if (readonly)
  1095. break;
  1096. if (selection.active) {
  1097. } else {
  1098. if (k.mod.shift) {
  1099. int cc = cursor.column;
  1100. if (cc>0 && cc<=text[cursor.line].length() && text[cursor.line][cursor.column-1]=='\t') {
  1101. //simple unindent
  1102. backspace_at_cursor();
  1103. }
  1104. } else {
  1105. //simple indent
  1106. _insert_text_at_cursor("\t");
  1107. }
  1108. }
  1109. } break;
  1110. case KEY_BACKSPACE: {
  1111. if (readonly)
  1112. break;
  1113. backspace_at_cursor();
  1114. } break;
  1115. case KEY_LEFT: {
  1116. if (k.mod.shift)
  1117. _pre_shift_selection();
  1118. #ifdef APPLE_STYLE_KEYS
  1119. if (k.mod.command) {
  1120. cursor_set_column(0);
  1121. } else if (k.mod.alt) {
  1122. #else
  1123. if (k.mod.alt) {
  1124. scancode_handled=false;
  1125. break;
  1126. } else if (k.mod.command) {
  1127. #endif
  1128. bool prev_char=false;
  1129. int cc=cursor.column;
  1130. while (cc>0) {
  1131. bool ischar=_is_text_char(text[cursor.line][cc-1]);
  1132. if (prev_char && !ischar)
  1133. break;
  1134. prev_char=ischar;
  1135. cc--;
  1136. }
  1137. cursor_set_column(cc);
  1138. } else if (cursor.column==0) {
  1139. if (cursor.line>0) {
  1140. cursor_set_line(cursor.line-1);
  1141. cursor_set_column(text[cursor.line].length());
  1142. }
  1143. } else {
  1144. cursor_set_column(cursor_get_column()-1);
  1145. }
  1146. if (k.mod.shift)
  1147. _post_shift_selection();
  1148. } break;
  1149. case KEY_RIGHT: {
  1150. if (k.mod.shift)
  1151. _pre_shift_selection();
  1152. #ifdef APPLE_STYLE_KEYS
  1153. if (k.mod.command) {
  1154. cursor_set_column(text[cursor.line].length());
  1155. } else if (k.mod.alt) {
  1156. #else
  1157. if (k.mod.alt) {
  1158. scancode_handled=false;
  1159. break;
  1160. } else if (k.mod.command) {
  1161. #endif
  1162. bool prev_char=false;
  1163. int cc=cursor.column;
  1164. while (cc<text[cursor.line].length()) {
  1165. bool ischar=_is_text_char(text[cursor.line][cc]);
  1166. if (prev_char && !ischar)
  1167. break;
  1168. prev_char=ischar;
  1169. cc++;
  1170. }
  1171. cursor_set_column(cc);
  1172. } else if (cursor.column==text[cursor.line].length()) {
  1173. if (cursor.line<text.size()-1) {
  1174. cursor_set_line(cursor.line+1);
  1175. cursor_set_column(0);
  1176. }
  1177. } else {
  1178. cursor_set_column(cursor_get_column()+1);
  1179. }
  1180. if (k.mod.shift)
  1181. _post_shift_selection();
  1182. } break;
  1183. case KEY_UP: {
  1184. if (k.mod.shift)
  1185. _pre_shift_selection();
  1186. if (k.mod.alt) {
  1187. scancode_handled=false;
  1188. break;
  1189. }
  1190. #ifdef APPLE_STYLE_KEYS
  1191. if (k.mod.command)
  1192. cursor_set_line(0);
  1193. else
  1194. #endif
  1195. cursor_set_line(cursor_get_line()-1);
  1196. if (k.mod.shift)
  1197. _post_shift_selection();
  1198. _cancel_code_hint();
  1199. } break;
  1200. case KEY_DOWN: {
  1201. if (k.mod.shift)
  1202. _pre_shift_selection();
  1203. if (k.mod.alt) {
  1204. scancode_handled=false;
  1205. break;
  1206. }
  1207. #ifdef APPLE_STYLE_KEYS
  1208. if (k.mod.command)
  1209. cursor_set_line(text.size()-1);
  1210. else
  1211. #endif
  1212. cursor_set_line(cursor_get_line()+1);
  1213. if (k.mod.shift)
  1214. _post_shift_selection();
  1215. _cancel_code_hint();
  1216. } break;
  1217. case KEY_DELETE: {
  1218. if (readonly)
  1219. break;
  1220. int curline_len = text[cursor.line].length();
  1221. if (cursor.line==text.size()-1 && cursor.column==curline_len)
  1222. break; //nothing to do
  1223. int next_line = cursor.column<curline_len?cursor.line:cursor.line+1;
  1224. int next_column = cursor.column<curline_len?(cursor.column+1):0;
  1225. _remove_text(cursor.line,cursor.column,next_line,next_column);
  1226. update();
  1227. } break;
  1228. #ifdef APPLE_STYLE_KEYS
  1229. case KEY_HOME: {
  1230. if (k.mod.shift)
  1231. _pre_shift_selection();
  1232. cursor_set_line(0);
  1233. if (k.mod.shift)
  1234. _post_shift_selection();
  1235. } break;
  1236. case KEY_END: {
  1237. if (k.mod.shift)
  1238. _pre_shift_selection();
  1239. cursor_set_line(text.size()-1);
  1240. if (k.mod.shift)
  1241. _post_shift_selection();
  1242. } break;
  1243. #else
  1244. case KEY_HOME: {
  1245. if (k.mod.shift)
  1246. _pre_shift_selection();
  1247. // compute whitespace symbols seq length
  1248. int current_line_whitespace_len = 0;
  1249. while(current_line_whitespace_len < text[cursor.line].length()) {
  1250. CharType c = text[cursor.line][current_line_whitespace_len];
  1251. if(c != '\t' && c != ' ')
  1252. break;
  1253. current_line_whitespace_len++;
  1254. }
  1255. if(cursor_get_column() == current_line_whitespace_len)
  1256. cursor_set_column(0);
  1257. else
  1258. cursor_set_column(current_line_whitespace_len);
  1259. if (k.mod.command)
  1260. cursor_set_line(0);
  1261. if (k.mod.shift)
  1262. _post_shift_selection();
  1263. } break;
  1264. case KEY_END: {
  1265. if (k.mod.shift)
  1266. _pre_shift_selection();
  1267. if (k.mod.command)
  1268. cursor_set_line(text.size()-1);
  1269. cursor_set_column(text[cursor.line].length());
  1270. if (k.mod.shift)
  1271. _post_shift_selection();
  1272. } break;
  1273. #endif
  1274. case KEY_PAGEUP: {
  1275. if (k.mod.shift)
  1276. _pre_shift_selection();
  1277. cursor_set_line(cursor_get_line()-get_visible_rows());
  1278. if (k.mod.shift)
  1279. _post_shift_selection();
  1280. } break;
  1281. case KEY_PAGEDOWN: {
  1282. if (k.mod.shift)
  1283. _pre_shift_selection();
  1284. cursor_set_line(cursor_get_line()+get_visible_rows());
  1285. if (k.mod.shift)
  1286. _post_shift_selection();
  1287. } break;
  1288. case KEY_A: {
  1289. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1290. scancode_handled=false;
  1291. break;
  1292. }
  1293. if (text.size()==1 && text[0].length()==0)
  1294. break;
  1295. selection.active=true;
  1296. selection.from_line=0;
  1297. selection.from_column=0;
  1298. selection.to_line=text.size()-1;
  1299. selection.to_column=text[selection.to_line].size();
  1300. selection.selecting_mode=Selection::MODE_NONE;
  1301. update();
  1302. } break;
  1303. case KEY_X: {
  1304. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1305. scancode_handled=false;
  1306. break;
  1307. }
  1308. if (!selection.active){
  1309. String clipboard = text[cursor.line];
  1310. OS::get_singleton()->set_clipboard(clipboard);
  1311. cursor_set_line(cursor.line);
  1312. cursor_set_column(0);
  1313. _remove_text(cursor.line,0,cursor.line,text[cursor.line].length());
  1314. backspace_at_cursor();
  1315. update();
  1316. cursor_set_line(cursor.line+1);
  1317. cut_copy_line = true;
  1318. }
  1319. else
  1320. {
  1321. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1322. OS::get_singleton()->set_clipboard(clipboard);
  1323. cursor_set_line(selection.from_line);
  1324. cursor_set_column(selection.from_column);
  1325. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1326. selection.active=false;
  1327. selection.selecting_mode=Selection::MODE_NONE;
  1328. update();
  1329. cut_copy_line = false;
  1330. }
  1331. } break;
  1332. case KEY_C: {
  1333. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1334. scancode_handled=false;
  1335. break;
  1336. }
  1337. if (!selection.active){
  1338. String clipboard = _base_get_text(cursor.line,0,cursor.line,text[cursor.line].length());
  1339. OS::get_singleton()->set_clipboard(clipboard);
  1340. cut_copy_line = true;
  1341. }
  1342. else{
  1343. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1344. OS::get_singleton()->set_clipboard(clipboard);
  1345. cut_copy_line = false;
  1346. }
  1347. } break;
  1348. case KEY_Z: {
  1349. if (!k.mod.command) {
  1350. scancode_handled=false;
  1351. break;
  1352. }
  1353. if (k.mod.shift)
  1354. redo();
  1355. else
  1356. undo();
  1357. } break;
  1358. case KEY_V: {
  1359. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1360. scancode_handled=false;
  1361. break;
  1362. }
  1363. String clipboard = OS::get_singleton()->get_clipboard();
  1364. if (selection.active) {
  1365. selection.active=false;
  1366. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1367. cursor_set_line(selection.from_line);
  1368. cursor_set_column(selection.from_column);
  1369. }
  1370. else if (cut_copy_line)
  1371. {
  1372. cursor_set_column(0);
  1373. String ins="\n";
  1374. clipboard += ins;
  1375. }
  1376. _insert_text_at_cursor(clipboard);
  1377. update();
  1378. } break;
  1379. case KEY_SPACE: {
  1380. if (completion_enabled && k.mod.command) {
  1381. query_code_comple();
  1382. scancode_handled=true;
  1383. } else {
  1384. scancode_handled=false;
  1385. }
  1386. } break;
  1387. case KEY_U:{
  1388. if (!k.mod.command || k.mod.shift || k.mod.alt) {
  1389. scancode_handled=false;
  1390. break;
  1391. }
  1392. else {
  1393. if (selection.active) {
  1394. int ini = selection.from_line;
  1395. int end = selection.to_line;
  1396. for (int i=ini; i<= end; i++)
  1397. {
  1398. if (text[i][0] == '#')
  1399. _remove_text(i,0,i,1);
  1400. }
  1401. }
  1402. else{
  1403. if (text[cursor.line][0] == '#')
  1404. _remove_text(cursor.line,0,cursor.line,1);
  1405. }
  1406. update();
  1407. }
  1408. break;}
  1409. default: {
  1410. scancode_handled=false;
  1411. } break;
  1412. }
  1413. if (scancode_handled)
  1414. accept_event();
  1415. /*
  1416. if (!scancode_handled && !k.mod.command && !k.mod.alt) {
  1417. if (k.unicode>=32) {
  1418. if (readonly)
  1419. break;
  1420. accept_event();
  1421. } else {
  1422. break;
  1423. }
  1424. }
  1425. */
  1426. if (!scancode_handled && !k.mod.command && !k.mod.alt) { //for german kbds
  1427. if (k.unicode>=32) {
  1428. if (readonly)
  1429. break;
  1430. const CharType chr[2] = {k.unicode, 0};
  1431. if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  1432. _consume_pair_symbol(chr[0]);
  1433. } else {
  1434. _insert_text_at_cursor(chr);
  1435. }
  1436. accept_event();
  1437. } else {
  1438. break;
  1439. }
  1440. }
  1441. if (!selection.selecting_test) {
  1442. selection.selecting_mode=Selection::MODE_NONE;
  1443. }
  1444. return;
  1445. } break;
  1446. }
  1447. }
  1448. void TextEdit::_pre_shift_selection() {
  1449. if (!selection.active || selection.selecting_mode!=Selection::MODE_SHIFT) {
  1450. selection.selecting_line=cursor.line;
  1451. selection.selecting_column=cursor.column;
  1452. selection.active=true;
  1453. selection.selecting_mode=Selection::MODE_SHIFT;
  1454. }
  1455. }
  1456. void TextEdit::_post_shift_selection() {
  1457. if (selection.active && selection.selecting_mode==Selection::MODE_SHIFT) {
  1458. select(selection.selecting_line,selection.selecting_column,cursor.line,cursor.column);
  1459. update();
  1460. }
  1461. selection.selecting_test=true;
  1462. }
  1463. /**** TEXT EDIT CORE API ****/
  1464. void TextEdit::_base_insert_text(int p_line, int p_char,const String& p_text,int &r_end_line,int &r_end_column) {
  1465. //save for undo...
  1466. ERR_FAIL_INDEX(p_line,text.size());
  1467. ERR_FAIL_COND(p_char<0);
  1468. /* STEP 1 add spaces if the char is greater than the end of the line */
  1469. while(p_char>text[p_line].length()) {
  1470. text.set(p_line,text[p_line]+String::chr(' '));
  1471. }
  1472. /* STEP 2 separate dest string in pre and post text */
  1473. String preinsert_text = text[p_line].substr(0,p_char);
  1474. String postinsert_text = text[p_line].substr(p_char,text[p_line].size());
  1475. /* STEP 3 remove \r from source text and separate in substrings */
  1476. //buh bye \r and split
  1477. Vector<String> substrings = p_text.replace("\r","").split("\n");
  1478. for(int i=0;i<substrings.size();i++) {
  1479. //insert the substrings
  1480. if (i==0) {
  1481. text.set(p_line,preinsert_text+substrings[i]);
  1482. } else {
  1483. text.insert(p_line+i,substrings[i]);
  1484. }
  1485. if (i==substrings.size()-1){
  1486. text.set(p_line+i,text[p_line+i]+postinsert_text);
  1487. }
  1488. }
  1489. r_end_line=p_line+substrings.size()-1;
  1490. r_end_column=text[r_end_line].length()-postinsert_text.length();
  1491. if (!text_changed_dirty && !setting_text) {
  1492. if (is_inside_tree())
  1493. MessageQueue::get_singleton()->push_call(this,"_text_changed_emit");
  1494. text_changed_dirty=true;
  1495. }
  1496. }
  1497. String TextEdit::_base_get_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) const {
  1498. ERR_FAIL_INDEX_V(p_from_line,text.size(),String());
  1499. ERR_FAIL_INDEX_V(p_from_column,text[p_from_line].length()+1,String());
  1500. ERR_FAIL_INDEX_V(p_to_line,text.size(),String());
  1501. ERR_FAIL_INDEX_V(p_to_column,text[p_to_line].length()+1,String());
  1502. ERR_FAIL_COND_V(p_to_line < p_from_line ,String()); // from > to
  1503. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column<p_from_column,String()); // from > to
  1504. String ret;
  1505. for(int i=p_from_line;i<=p_to_line;i++) {
  1506. int begin = (i==p_from_line)?p_from_column:0;
  1507. int end = (i==p_to_line)?p_to_column:text[i].length();
  1508. if (i>p_from_line)
  1509. ret+="\n";
  1510. ret+=text[i].substr(begin,end-begin);
  1511. }
  1512. return ret;
  1513. }
  1514. void TextEdit::_base_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) {
  1515. ERR_FAIL_INDEX(p_from_line,text.size());
  1516. ERR_FAIL_INDEX(p_from_column,text[p_from_line].length()+1);
  1517. ERR_FAIL_INDEX(p_to_line,text.size());
  1518. ERR_FAIL_INDEX(p_to_column,text[p_to_line].length()+1);
  1519. ERR_FAIL_COND(p_to_line < p_from_line ); // from > to
  1520. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column<p_from_column); // from > to
  1521. String pre_text = text[p_from_line].substr(0,p_from_column);
  1522. String post_text = text[p_to_line].substr(p_to_column,text[p_to_line].length());
  1523. for(int i=p_from_line;i<p_to_line;i++) {
  1524. text.remove(p_from_line+1);
  1525. }
  1526. text.set(p_from_line,pre_text+post_text);
  1527. if (!text_changed_dirty && !setting_text) {
  1528. if (is_inside_tree())
  1529. MessageQueue::get_singleton()->push_call(this,"_text_changed_emit");
  1530. text_changed_dirty=true;
  1531. }
  1532. }
  1533. void TextEdit::_insert_text(int p_line, int p_char,const String& p_text,int *r_end_line,int *r_end_column) {
  1534. if (!setting_text)
  1535. idle_detect->start();
  1536. if (undo_enabled) {
  1537. _clear_redo();
  1538. }
  1539. int retline,retchar;
  1540. _base_insert_text(p_line,p_char,p_text,retline,retchar);
  1541. if (r_end_line)
  1542. *r_end_line=retline;
  1543. if (r_end_column)
  1544. *r_end_column=retchar;
  1545. if (!undo_enabled)
  1546. return;
  1547. /* UNDO!! */
  1548. TextOperation op;
  1549. op.type=TextOperation::TYPE_INSERT;
  1550. op.from_line=p_line;
  1551. op.from_column=p_char;
  1552. op.to_line=retline;
  1553. op.to_column=retchar;
  1554. op.text=p_text;
  1555. op.version=++version;
  1556. op.chain_forward=false;
  1557. op.chain_backward=false;
  1558. //see if it shold just be set as current op
  1559. if (current_op.type!=op.type) {
  1560. _push_current_op();
  1561. current_op=op;
  1562. return; //set as current op, return
  1563. }
  1564. //see if it can be merged
  1565. if (current_op.to_line!=p_line || current_op.to_column!=p_char) {
  1566. _push_current_op();
  1567. current_op=op;
  1568. return; //set as current op, return
  1569. }
  1570. //merge current op
  1571. current_op.text+=p_text;
  1572. current_op.to_column=retchar;
  1573. current_op.to_line=retline;
  1574. current_op.version=op.version;
  1575. }
  1576. void TextEdit::_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) {
  1577. if (!setting_text)
  1578. idle_detect->start();
  1579. String text;
  1580. if (undo_enabled) {
  1581. _clear_redo();
  1582. text=_base_get_text(p_from_line,p_from_column,p_to_line,p_to_column);
  1583. }
  1584. _base_remove_text(p_from_line,p_from_column,p_to_line,p_to_column);
  1585. if (!undo_enabled)
  1586. return;
  1587. /* UNDO!! */
  1588. TextOperation op;
  1589. op.type=TextOperation::TYPE_REMOVE;
  1590. op.from_line=p_from_line;
  1591. op.from_column=p_from_column;
  1592. op.to_line=p_to_line;
  1593. op.to_column=p_to_column;
  1594. op.text=text;
  1595. op.version=++version;
  1596. op.chain_forward=false;
  1597. op.chain_backward=false;
  1598. //see if it shold just be set as current op
  1599. if (current_op.type!=op.type) {
  1600. _push_current_op();
  1601. current_op=op;
  1602. return; //set as current op, return
  1603. }
  1604. //see if it can be merged
  1605. if (current_op.from_line==p_to_line && current_op.from_column==p_to_column) {
  1606. //basckace or similar
  1607. current_op.text=text+current_op.text;
  1608. current_op.from_line=p_from_line;
  1609. current_op.from_column=p_from_column;
  1610. return; //update current op
  1611. }
  1612. if (current_op.from_line==p_from_line && current_op.from_column==p_from_column) {
  1613. //current_op.text=text+current_op.text;
  1614. //current_op.from_line=p_from_line;
  1615. //current_op.from_column=p_from_column;
  1616. //return; //update current op
  1617. }
  1618. _push_current_op();
  1619. current_op=op;
  1620. }
  1621. void TextEdit::_insert_text_at_cursor(const String& p_text) {
  1622. int new_column,new_line;
  1623. _insert_text(cursor.line,cursor.column,p_text,&new_line,&new_column);
  1624. cursor_set_line(new_line);
  1625. cursor_set_column(new_column);
  1626. update();
  1627. }
  1628. int TextEdit::get_char_count() {
  1629. int totalsize=0;
  1630. for (int i=0;i<text.size();i++) {
  1631. if (i>0)
  1632. totalsize++; // incliude \n
  1633. totalsize+=text[i].length();
  1634. }
  1635. return totalsize; // omit last \n
  1636. }
  1637. Size2 TextEdit::get_minimum_size() {
  1638. return cache.style_normal->get_minimum_size();
  1639. }
  1640. int TextEdit::get_visible_rows() const {
  1641. int total=cache.size.height;
  1642. total-=cache.style_normal->get_minimum_size().height;
  1643. total/=get_row_height();
  1644. return total;
  1645. }
  1646. void TextEdit::adjust_viewport_to_cursor() {
  1647. if (cursor.line_ofs>cursor.line)
  1648. cursor.line_ofs=cursor.line;
  1649. int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w;
  1650. if (v_scroll->is_visible())
  1651. visible_width-=v_scroll->get_combined_minimum_size().width;
  1652. visible_width-=20; // give it a little more space
  1653. //printf("rowofs %i, visrows %i, cursor.line %i\n",cursor.line_ofs,get_visible_rows(),cursor.line);
  1654. int visible_rows = get_visible_rows();
  1655. if (h_scroll->is_visible())
  1656. visible_rows-=((h_scroll->get_combined_minimum_size().height-1)/get_row_height());
  1657. if (cursor.line>=(cursor.line_ofs+visible_rows))
  1658. cursor.line_ofs=cursor.line-visible_rows+1;
  1659. if (cursor.line<cursor.line_ofs)
  1660. cursor.line_ofs=cursor.line;
  1661. int cursor_x = get_column_x_offset( cursor.column, text[cursor.line] );
  1662. if (cursor_x>(cursor.x_ofs+visible_width))
  1663. cursor.x_ofs=cursor_x-visible_width+1;
  1664. if (cursor_x < cursor.x_ofs)
  1665. cursor.x_ofs=cursor_x;
  1666. update();
  1667. /*
  1668. get_range()->set_max(text.size());
  1669. get_range()->set_page(get_visible_rows());
  1670. get_range()->set((int)cursor.line_ofs);
  1671. */
  1672. }
  1673. void TextEdit::cursor_set_column(int p_col) {
  1674. if (p_col<0)
  1675. p_col=0;
  1676. cursor.column=p_col;
  1677. if (cursor.column > get_line( cursor.line ).length())
  1678. cursor.column=get_line( cursor.line ).length();
  1679. cursor.last_fit_x=get_column_x_offset(cursor.column,get_line(cursor.line));
  1680. adjust_viewport_to_cursor();
  1681. if (!cursor_changed_dirty) {
  1682. if (is_inside_tree())
  1683. MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit");
  1684. cursor_changed_dirty=true;
  1685. }
  1686. }
  1687. void TextEdit::cursor_set_line(int p_row) {
  1688. if (setting_row)
  1689. return;
  1690. setting_row=true;
  1691. if (p_row<0)
  1692. p_row=0;
  1693. if (p_row>=(int)text.size())
  1694. p_row=(int)text.size()-1;
  1695. cursor.line=p_row;
  1696. cursor.column=get_char_pos_for( cursor.last_fit_x, get_line( cursor.line) );
  1697. adjust_viewport_to_cursor();
  1698. setting_row=false;
  1699. if (!cursor_changed_dirty) {
  1700. if (is_inside_tree())
  1701. MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit");
  1702. cursor_changed_dirty=true;
  1703. }
  1704. }
  1705. int TextEdit::cursor_get_column() const {
  1706. return cursor.column;
  1707. }
  1708. int TextEdit::cursor_get_line() const {
  1709. return cursor.line;
  1710. }
  1711. void TextEdit::_scroll_moved(double p_to_val) {
  1712. if (updating_scrolls)
  1713. return;
  1714. if (h_scroll->is_visible())
  1715. cursor.x_ofs=h_scroll->get_val();
  1716. if (v_scroll->is_visible())
  1717. cursor.line_ofs=v_scroll->get_val();
  1718. update();
  1719. }
  1720. int TextEdit::get_row_height() const {
  1721. return cache.font->get_height()+cache.line_spacing;
  1722. }
  1723. int TextEdit::get_char_pos_for(int p_px,String p_str) const {
  1724. int px=0;
  1725. int c=0;
  1726. int tab_w = cache.font->get_char_size(' ').width*tab_size;
  1727. while (c<p_str.length()) {
  1728. int w=0;
  1729. if (p_str[c]=='\t') {
  1730. int left = px%tab_w;
  1731. if (left==0)
  1732. w=tab_w;
  1733. else
  1734. w=tab_w-px%tab_w; // is right...
  1735. } else {
  1736. w=cache.font->get_char_size(p_str[c],p_str[c+1]).width;
  1737. }
  1738. if (p_px<(px+w/2))
  1739. break;
  1740. px+=w;
  1741. c++;
  1742. }
  1743. return c;
  1744. }
  1745. int TextEdit::get_column_x_offset(int p_char,String p_str) {
  1746. int px=0;
  1747. int tab_w = cache.font->get_char_size(' ').width*tab_size;
  1748. for (int i=0;i<p_char;i++) {
  1749. if (i>=p_str.length())
  1750. break;
  1751. if (p_str[i]=='\t') {
  1752. int left = px%tab_w;
  1753. if (left==0)
  1754. px+=tab_w;
  1755. else
  1756. px+=tab_w-px%tab_w; // is right...
  1757. } else {
  1758. px+=cache.font->get_char_size(p_str[i],p_str[i+1]).width;
  1759. }
  1760. }
  1761. return px;
  1762. }
  1763. void TextEdit::insert_text_at_cursor(const String& p_text) {
  1764. if (selection.active) {
  1765. cursor_set_line(selection.from_line);
  1766. cursor_set_column(selection.from_column);
  1767. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1768. selection.active=false;
  1769. selection.selecting_mode=Selection::MODE_NONE;
  1770. }
  1771. _insert_text_at_cursor(p_text);
  1772. update();
  1773. }
  1774. Control::CursorShape TextEdit::get_cursor_shape(const Point2& p_pos) const {
  1775. if(completion_active && completion_rect.has_point(p_pos)) {
  1776. return CURSOR_ARROW;
  1777. }
  1778. return CURSOR_IBEAM;
  1779. }
  1780. void TextEdit::set_text(String p_text){
  1781. setting_text=true;
  1782. _clear();
  1783. _insert_text_at_cursor(p_text);
  1784. cursor.column=0;
  1785. cursor.line=0;
  1786. cursor.x_ofs=0;
  1787. cursor.line_ofs=0;
  1788. cursor.last_fit_x=0;
  1789. cursor_set_line(0);
  1790. cursor_set_column(0);
  1791. update();
  1792. setting_text=false;
  1793. //get_range()->set(0);
  1794. };
  1795. String TextEdit::get_text() {
  1796. String longthing;
  1797. int len = text.size();
  1798. for (int i=0;i<len;i++) {
  1799. longthing+=text[i];
  1800. if (i!=len-1)
  1801. longthing+="\n";
  1802. }
  1803. return longthing;
  1804. };
  1805. String TextEdit::get_text_for_completion() {
  1806. String longthing;
  1807. int len = text.size();
  1808. for (int i=0;i<len;i++) {
  1809. if (i==cursor.line) {
  1810. longthing+=text[i].substr(0,cursor.column);
  1811. longthing+=String::chr(0xFFFF); //not unicode, represents the cursor
  1812. longthing+=text[i].substr(cursor.column,text[i].size());
  1813. } else {
  1814. longthing+=text[i];
  1815. }
  1816. if (i!=len-1)
  1817. longthing+="\n";
  1818. }
  1819. return longthing;
  1820. };
  1821. String TextEdit::get_line(int line) const {
  1822. if (line<0 || line>=text.size())
  1823. return "";
  1824. return text[line];
  1825. };
  1826. void TextEdit::_clear() {
  1827. clear_undo_history();
  1828. text.clear();
  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. }
  1835. void TextEdit::clear() {
  1836. setting_text=true;
  1837. _clear();
  1838. setting_text=false;
  1839. };
  1840. void TextEdit::set_readonly(bool p_readonly) {
  1841. readonly=p_readonly;
  1842. }
  1843. void TextEdit::set_wrap(bool p_wrap) {
  1844. wrap=p_wrap;
  1845. }
  1846. void TextEdit::set_max_chars(int p_max_chars) {
  1847. max_chars=p_max_chars;
  1848. }
  1849. void TextEdit::_update_caches() {
  1850. cache.style_normal=get_stylebox("normal");
  1851. cache.style_focus=get_stylebox("focus");
  1852. cache.font=get_font("font");
  1853. cache.font_color=get_color("font_color");
  1854. cache.font_selected_color=get_color("font_selected_color");
  1855. cache.keyword_color=get_color("keyword_color");
  1856. cache.selection_color=get_color("selection_color");
  1857. cache.mark_color=get_color("mark_color");
  1858. cache.current_line_color=get_color("current_line_color");
  1859. cache.breakpoint_color=get_color("breakpoint_color");
  1860. cache.brace_mismatch_color=get_color("brace_mismatch_color");
  1861. cache.line_spacing=get_constant("line_spacing");
  1862. cache.row_height = cache.font->get_height() + cache.line_spacing;
  1863. cache.tab_icon=get_icon("tab");
  1864. text.set_font(cache.font);
  1865. }
  1866. void TextEdit::clear_colors() {
  1867. keywords.clear();
  1868. color_regions.clear();;
  1869. text.clear_caches();
  1870. custom_bg_color=Color(0,0,0,0);
  1871. }
  1872. void TextEdit::set_custom_bg_color(const Color& p_color) {
  1873. custom_bg_color=p_color;
  1874. update();
  1875. }
  1876. void TextEdit::add_keyword_color(const String& p_keyword,const Color& p_color) {
  1877. keywords[p_keyword]=p_color;
  1878. update();
  1879. }
  1880. void TextEdit::add_color_region(const String& p_begin_key,const String& p_end_key,const Color &p_color,bool p_line_only) {
  1881. color_regions.push_back(ColorRegion(p_begin_key,p_end_key,p_color,p_line_only));
  1882. text.clear_caches();
  1883. update();
  1884. }
  1885. void TextEdit::set_symbol_color(const Color& p_color) {
  1886. symbol_color=p_color;
  1887. update();
  1888. }
  1889. void TextEdit::set_syntax_coloring(bool p_enabled) {
  1890. syntax_coloring=p_enabled;
  1891. update();
  1892. }
  1893. bool TextEdit::is_syntax_coloring_enabled() const {
  1894. return syntax_coloring;
  1895. }
  1896. void TextEdit::cut() {
  1897. if (!selection.active)
  1898. return;
  1899. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1900. OS::get_singleton()->set_clipboard(clipboard);
  1901. cursor_set_line(selection.from_line);
  1902. cursor_set_column(selection.from_column);
  1903. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1904. selection.active=false;
  1905. selection.selecting_mode=Selection::MODE_NONE;
  1906. update();
  1907. }
  1908. void TextEdit::copy() {
  1909. if (!selection.active)
  1910. return;
  1911. String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1912. OS::get_singleton()->set_clipboard(clipboard);
  1913. }
  1914. void TextEdit::paste() {
  1915. if (selection.active) {
  1916. cursor_set_line(selection.from_line);
  1917. cursor_set_column(selection.from_column);
  1918. _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1919. selection.active=false;
  1920. selection.selecting_mode=Selection::MODE_NONE;
  1921. }
  1922. String clipboard = OS::get_singleton()->get_clipboard();
  1923. _insert_text_at_cursor(clipboard);
  1924. update();
  1925. }
  1926. void TextEdit::select_all() {
  1927. if (text.size()==1 && text[0].length()==0)
  1928. return;
  1929. selection.active=true;
  1930. selection.from_line=0;
  1931. selection.from_column=0;
  1932. selection.to_line=text.size()-1;
  1933. selection.to_column=text[selection.to_line].size();
  1934. selection.selecting_mode=Selection::MODE_NONE;
  1935. update();
  1936. }
  1937. void TextEdit::deselect() {
  1938. selection.active=false;
  1939. update();
  1940. }
  1941. void TextEdit::select(int p_from_line,int p_from_column,int p_to_line,int p_to_column) {
  1942. if (p_from_line>=text.size())
  1943. p_from_line=text.size()-1;
  1944. if (p_from_column>=text[p_from_line].length())
  1945. p_from_column=text[p_from_line].length();
  1946. if (p_to_line>=text.size())
  1947. p_to_line=text.size()-1;
  1948. if (p_to_column>=text[p_to_line].length())
  1949. p_to_column=text[p_to_line].length();
  1950. selection.from_line=p_from_line;
  1951. selection.from_column=p_from_column;
  1952. selection.to_line=p_to_line;
  1953. selection.to_column=p_to_column;
  1954. selection.active=true;
  1955. if (selection.from_line==selection.to_line) {
  1956. if (selection.from_column==selection.to_column) {
  1957. selection.active=false;
  1958. } else if (selection.from_column>selection.to_column) {
  1959. SWAP( selection.from_column, selection.to_column );
  1960. }
  1961. } else if (selection.from_line>selection.to_line) {
  1962. SWAP( selection.from_line, selection.to_line );
  1963. SWAP( selection.from_column, selection.to_column );
  1964. }
  1965. update();
  1966. }
  1967. bool TextEdit::is_selection_active() const {
  1968. return selection.active;
  1969. }
  1970. int TextEdit::get_selection_from_line() const {
  1971. ERR_FAIL_COND_V(!selection.active,-1);
  1972. return selection.from_line;
  1973. }
  1974. int TextEdit::get_selection_from_column() const {
  1975. ERR_FAIL_COND_V(!selection.active,-1);
  1976. return selection.from_column;
  1977. }
  1978. int TextEdit::get_selection_to_line() const {
  1979. ERR_FAIL_COND_V(!selection.active,-1);
  1980. return selection.to_line;
  1981. }
  1982. int TextEdit::get_selection_to_column() const {
  1983. ERR_FAIL_COND_V(!selection.active,-1);
  1984. return selection.to_column;
  1985. }
  1986. String TextEdit::get_selection_text() const {
  1987. if (!selection.active)
  1988. return "";
  1989. return _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
  1990. }
  1991. String TextEdit::get_word_under_cursor() const {
  1992. int prev_cc = cursor.column;
  1993. while(prev_cc >0) {
  1994. bool is_char = _is_text_char(text[cursor.line][prev_cc-1]);
  1995. if (!is_char)
  1996. break;
  1997. --prev_cc;
  1998. }
  1999. int next_cc = cursor.column;
  2000. while(next_cc<text[cursor.line].length()) {
  2001. bool is_char = _is_text_char(text[cursor.line][next_cc]);
  2002. if(!is_char)
  2003. break;
  2004. ++ next_cc;
  2005. }
  2006. if (prev_cc == cursor.column || next_cc == cursor.column)
  2007. return "";
  2008. return text[cursor.line].substr(prev_cc, next_cc-prev_cc);
  2009. }
  2010. DVector<int> TextEdit::_search_bind(const String &p_key,uint32_t p_search_flags, int p_from_line,int p_from_column) const {
  2011. int col,line;
  2012. if (search(p_key,p_search_flags,p_from_line,p_from_column,col,line)) {
  2013. DVector<int> result;
  2014. result.resize(2);
  2015. result.set(0,line);
  2016. result.set(1,col);
  2017. return result;
  2018. } else {
  2019. return DVector<int>();
  2020. }
  2021. }
  2022. 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 {
  2023. if (p_key.length()==0)
  2024. return false;
  2025. ERR_FAIL_INDEX_V(p_from_line,text.size(),false);
  2026. ERR_FAIL_INDEX_V(p_from_column,text[p_from_line].length()+1,false);
  2027. //search through the whole documment, but start by current line
  2028. int line=-1;
  2029. int pos=-1;
  2030. line=p_from_line;
  2031. for(int i=0;i<text.size()+1;i++) {
  2032. //backwards is broken...
  2033. //int idx=(p_search_flags&SEARCH_BACKWARDS)?(text.size()-i):i; //do backwards seearch
  2034. if (line<0) {
  2035. line=text.size()-1;
  2036. }
  2037. if (line==text.size()) {
  2038. line=0;
  2039. }
  2040. String text_line = text[line];
  2041. int from_column=0;
  2042. if (line==p_from_line) {
  2043. if (i==text.size()) {
  2044. //wrapped
  2045. if (p_search_flags&SEARCH_BACKWARDS) {
  2046. text_line=text_line.substr(from_column,text_line.length());
  2047. from_column=text_line.length();
  2048. } else {
  2049. text_line=text_line.substr(0,from_column);
  2050. from_column=0;
  2051. }
  2052. } else {
  2053. from_column=p_from_column;
  2054. }
  2055. } else {
  2056. //text_line=text_line.substr(0,p_from_column); //wrap around for missing begining.
  2057. if (p_search_flags&SEARCH_BACKWARDS)
  2058. from_column=text_line.length()-1;
  2059. else
  2060. from_column=0;
  2061. }
  2062. pos=-1;
  2063. if (!(p_search_flags&SEARCH_BACKWARDS)) {
  2064. pos = (p_search_flags&SEARCH_MATCH_CASE)?text_line.find(p_key,from_column):text_line.findn(p_key,from_column);
  2065. } else {
  2066. pos = (p_search_flags&SEARCH_MATCH_CASE)?text_line.rfind(p_key,from_column):text_line.rfindn(p_key,from_column);
  2067. }
  2068. if (pos!=-1 && (p_search_flags&SEARCH_WHOLE_WORDS)) {
  2069. //validate for whole words
  2070. if (pos>0 && _is_text_char(text_line[pos-1]))
  2071. pos=-1;
  2072. else if (_is_text_char(text_line[pos+p_key.length()]))
  2073. pos=-1;
  2074. }
  2075. if (pos!=-1)
  2076. break;
  2077. if (p_search_flags&SEARCH_BACKWARDS)
  2078. line--;
  2079. else
  2080. line++;
  2081. }
  2082. if (pos==-1) {
  2083. r_line=-1;
  2084. r_column=-1;
  2085. return false;
  2086. }
  2087. r_line=line;
  2088. r_column=pos;
  2089. return true;
  2090. }
  2091. void TextEdit::_cursor_changed_emit() {
  2092. emit_signal("cursor_changed");
  2093. cursor_changed_dirty=false;
  2094. }
  2095. void TextEdit::_text_changed_emit() {
  2096. emit_signal("text_changed");
  2097. text_changed_dirty=false;
  2098. }
  2099. void TextEdit::set_line_as_marked(int p_line,bool p_marked) {
  2100. ERR_FAIL_INDEX(p_line,text.size());
  2101. text.set_marked(p_line,p_marked);
  2102. update();
  2103. }
  2104. bool TextEdit::is_line_set_as_breakpoint(int p_line) const {
  2105. ERR_FAIL_INDEX_V(p_line,text.size(),false);
  2106. return text.is_breakpoint(p_line);
  2107. }
  2108. void TextEdit::set_line_as_breakpoint(int p_line,bool p_breakpoint) {
  2109. ERR_FAIL_INDEX(p_line,text.size());
  2110. text.set_breakpoint(p_line,p_breakpoint);
  2111. update();
  2112. }
  2113. void TextEdit::get_breakpoints(List<int> *p_breakpoints) const {
  2114. for(int i=0;i<text.size();i++) {
  2115. if (text.is_breakpoint(i))
  2116. p_breakpoints->push_back(i);
  2117. }
  2118. }
  2119. int TextEdit::get_line_count() const {
  2120. return text.size();
  2121. }
  2122. void TextEdit::_do_text_op(const TextOperation& p_op, bool p_reverse) {
  2123. ERR_FAIL_COND(p_op.type==TextOperation::TYPE_NONE);
  2124. bool insert = p_op.type==TextOperation::TYPE_INSERT;
  2125. if (p_reverse)
  2126. insert=!insert;
  2127. if (insert) {
  2128. int check_line;
  2129. int check_column;
  2130. _base_insert_text(p_op.from_line,p_op.from_column,p_op.text,check_line,check_column);
  2131. ERR_FAIL_COND( check_line != p_op.to_line ); // BUG
  2132. ERR_FAIL_COND( check_column != p_op.to_column ); // BUG
  2133. } else {
  2134. _base_remove_text(p_op.from_line,p_op.from_column,p_op.to_line,p_op.to_column);
  2135. }
  2136. }
  2137. void TextEdit::_clear_redo() {
  2138. if (undo_stack_pos==NULL)
  2139. return; //nothing to clear
  2140. _push_current_op();
  2141. while (undo_stack_pos) {
  2142. List<TextOperation>::Element *elem = undo_stack_pos;
  2143. undo_stack_pos=undo_stack_pos->next();
  2144. undo_stack.erase(elem);
  2145. }
  2146. }
  2147. void TextEdit::undo() {
  2148. _push_current_op();
  2149. if (undo_stack_pos==NULL) {
  2150. if (!undo_stack.size())
  2151. return; //nothing to undo
  2152. undo_stack_pos=undo_stack.back();
  2153. } else if (undo_stack_pos==undo_stack.front())
  2154. return; // at the bottom of the undo stack
  2155. else
  2156. undo_stack_pos=undo_stack_pos->prev();
  2157. _do_text_op( undo_stack_pos->get(),true);
  2158. if(undo_stack_pos->get().chain_backward) {
  2159. do {
  2160. undo_stack_pos = undo_stack_pos->prev();
  2161. _do_text_op(undo_stack_pos->get(), true);
  2162. } while(!undo_stack_pos->get().chain_forward);
  2163. }
  2164. cursor_set_line(undo_stack_pos->get().from_line);
  2165. cursor_set_column(undo_stack_pos->get().from_column);
  2166. update();
  2167. }
  2168. void TextEdit::redo() {
  2169. _push_current_op();
  2170. if (undo_stack_pos==NULL)
  2171. return; //nothing to do.
  2172. _do_text_op(undo_stack_pos->get(), false);
  2173. if(undo_stack_pos->get().chain_forward) {
  2174. do {
  2175. undo_stack_pos=undo_stack_pos->next();
  2176. _do_text_op(undo_stack_pos->get(), false);
  2177. } while(!undo_stack_pos->get().chain_backward);
  2178. }
  2179. cursor_set_line(undo_stack_pos->get().from_line);
  2180. cursor_set_column(undo_stack_pos->get().from_column);
  2181. undo_stack_pos=undo_stack_pos->next();
  2182. update();
  2183. }
  2184. void TextEdit::clear_undo_history() {
  2185. saved_version=0;
  2186. current_op.type=TextOperation::TYPE_NONE;
  2187. undo_stack_pos=NULL;
  2188. undo_stack.clear();
  2189. }
  2190. void TextEdit::_begin_compex_operation() {
  2191. _push_current_op();
  2192. next_operation_is_complex=true;
  2193. }
  2194. void TextEdit::_end_compex_operation() {
  2195. _push_current_op();
  2196. ERR_FAIL_COND(undo_stack.size() == 0);
  2197. if(undo_stack.back()->get().chain_forward) {
  2198. undo_stack.back()->get().chain_forward=false;
  2199. return;
  2200. }
  2201. undo_stack.back()->get().chain_backward=true;
  2202. }
  2203. void TextEdit::_push_current_op() {
  2204. if (current_op.type==TextOperation::TYPE_NONE)
  2205. return; // do nothing
  2206. if(next_operation_is_complex) {
  2207. current_op.chain_forward=true;
  2208. next_operation_is_complex=false;
  2209. }
  2210. undo_stack.push_back(current_op);
  2211. current_op.type=TextOperation::TYPE_NONE;
  2212. current_op.text="";
  2213. current_op.chain_forward=false;
  2214. }
  2215. void TextEdit::set_draw_tabs(bool p_draw) {
  2216. draw_tabs=p_draw;
  2217. }
  2218. bool TextEdit::is_drawing_tabs() const{
  2219. return draw_tabs;
  2220. }
  2221. uint32_t TextEdit::get_version() const {
  2222. return current_op.version;
  2223. }
  2224. uint32_t TextEdit::get_saved_version() const {
  2225. return saved_version;
  2226. }
  2227. void TextEdit::tag_saved_version() {
  2228. saved_version=get_version();
  2229. }
  2230. int TextEdit::get_v_scroll() const {
  2231. return v_scroll->get_val();
  2232. }
  2233. void TextEdit::set_v_scroll(int p_scroll) {
  2234. v_scroll->set_val(p_scroll);
  2235. cursor.line_ofs=p_scroll;
  2236. }
  2237. int TextEdit::get_h_scroll() const {
  2238. return h_scroll->get_val();
  2239. }
  2240. void TextEdit::set_h_scroll(int p_scroll) {
  2241. h_scroll->set_val(p_scroll);
  2242. }
  2243. void TextEdit::set_completion(bool p_enabled,const Vector<String>& p_prefixes) {
  2244. completion_prefixes.clear();
  2245. completion_enabled=p_enabled;
  2246. for(int i=0;i<p_prefixes.size();i++)
  2247. completion_prefixes.insert(p_prefixes[i]);
  2248. }
  2249. void TextEdit::_confirm_completion() {
  2250. String remaining=completion_current.substr(completion_base.length(),completion_current.length()-completion_base.length());
  2251. String l = text[cursor.line];
  2252. bool same=true;
  2253. //if what is going to be inserted is the same as what it is, don't change it
  2254. for(int i=0;i<remaining.length();i++) {
  2255. int c=i+cursor.column;
  2256. if (c>=l.length() || l[c]!=remaining[i]) {
  2257. same=false;
  2258. break;
  2259. }
  2260. }
  2261. if (same)
  2262. cursor_set_column(cursor.column+remaining.length());
  2263. else {
  2264. insert_text_at_cursor(remaining);
  2265. if (remaining.ends_with("(") && auto_brace_completion_enabled) {
  2266. insert_text_at_cursor(")");
  2267. cursor.column--;
  2268. }
  2269. }
  2270. _cancel_completion();
  2271. }
  2272. void TextEdit::_cancel_code_hint() {
  2273. completion_hint="";
  2274. update();
  2275. }
  2276. void TextEdit::_cancel_completion() {
  2277. if (!completion_active)
  2278. return;
  2279. completion_active=false;
  2280. update();
  2281. }
  2282. static bool _is_completable(CharType c) {
  2283. return !_is_symbol(c) || c=='"' || c=='\'';
  2284. }
  2285. void TextEdit::_update_completion_candidates() {
  2286. String l = text[cursor.line];
  2287. int cofs = CLAMP(cursor.column,0,l.length());
  2288. String s;
  2289. while(cofs>0 && l[cofs-1]>32 && _is_completable(l[cofs-1])) {
  2290. s=String::chr(l[cofs-1])+s;
  2291. if (l[cofs-1]=='\'' || l[cofs-1]=='"')
  2292. break;
  2293. cofs--;
  2294. }
  2295. update();
  2296. if (s=="" && (cofs==0 || !completion_prefixes.has(String::chr(l[cofs-1])))) {
  2297. //none to complete, cancel
  2298. _cancel_completion();
  2299. return;
  2300. }
  2301. completion_options.clear();
  2302. completion_index=0;
  2303. completion_base=s;
  2304. int ci_match=0;
  2305. for(int i=0;i<completion_strings.size();i++) {
  2306. if (completion_strings[i].begins_with(s)) {
  2307. completion_options.push_back(completion_strings[i]);
  2308. int m=0;
  2309. int max=MIN(completion_current.length(),completion_strings[i].length());
  2310. if (max<ci_match)
  2311. continue;
  2312. for(int j=0;j<max;j++) {
  2313. if (j>=completion_strings[i].length())
  2314. break;
  2315. if (completion_current[j]!=completion_strings[i][j])
  2316. break;
  2317. m++;
  2318. }
  2319. if (m>ci_match) {
  2320. ci_match=m;
  2321. completion_index=completion_options.size()-1;
  2322. }
  2323. }
  2324. }
  2325. if (completion_options.size()==0) {
  2326. //no options to complete, cancel
  2327. _cancel_completion();
  2328. return;
  2329. }
  2330. completion_current=completion_options[completion_index];
  2331. #if 0 // even there's only one option, user still get the chance to choose using it or not
  2332. if (completion_options.size()==1) {
  2333. //one option to complete, just complete it automagically
  2334. _confirm_completion();
  2335. // insert_text_at_cursor(completion_options[0].substr(s.length(),completion_options[0].length()-s.length()));
  2336. _cancel_completion();
  2337. return;
  2338. }
  2339. #endif
  2340. if (completion_options.size()==1 && s==completion_options[0])
  2341. _cancel_completion();
  2342. completion_enabled=true;
  2343. }
  2344. void TextEdit::query_code_comple() {
  2345. String l = text[cursor.line];
  2346. int ofs = CLAMP(cursor.column,0,l.length());
  2347. if (ofs>0 && (_is_completable(l[ofs-1]) || completion_prefixes.has(String::chr(l[ofs-1]))))
  2348. emit_signal("request_completion");
  2349. }
  2350. void TextEdit::set_code_hint(const String& p_hint) {
  2351. completion_hint=p_hint;
  2352. completion_hint_offset=-0xFFFF;
  2353. update();
  2354. }
  2355. void TextEdit::code_complete(const Vector<String> &p_strings) {
  2356. completion_strings=p_strings;
  2357. completion_active=true;
  2358. completion_current="";
  2359. completion_index=0;
  2360. _update_completion_candidates();
  2361. //
  2362. }
  2363. String TextEdit::get_tooltip(const Point2& p_pos) const {
  2364. if (!tooltip_obj)
  2365. return Control::get_tooltip(p_pos);
  2366. int row,col;
  2367. if (!_get_mouse_pos(p_pos, row,col)) {
  2368. return Control::get_tooltip(p_pos);
  2369. }
  2370. String s = text[row];
  2371. if (s.length()==0)
  2372. return Control::get_tooltip(p_pos);
  2373. int beg=CLAMP(col,0,s.length());
  2374. int end=beg;
  2375. if (s[beg]>32 || beg==s.length()) {
  2376. bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
  2377. while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) {
  2378. beg--;
  2379. }
  2380. while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) {
  2381. end++;
  2382. }
  2383. if (end<s.length())
  2384. end+=1;
  2385. String tt = tooltip_obj->call(tooltip_func,s.substr(beg,end-beg),tooltip_ud);
  2386. return tt;
  2387. }
  2388. return Control::get_tooltip(p_pos);
  2389. }
  2390. void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName& p_function,const Variant& p_udata) {
  2391. tooltip_obj=p_obj;
  2392. tooltip_func=p_function;
  2393. tooltip_ud=p_udata;
  2394. }
  2395. void TextEdit::set_line(int line, String new_text)
  2396. {
  2397. if (line < 0 || line > text.size())
  2398. return;
  2399. _remove_text(line, 0, line, text[line].length());
  2400. _insert_text(line, 0, new_text);
  2401. }
  2402. void TextEdit::insert_at(const String &p_text, int at)
  2403. {
  2404. cursor_set_column(0);
  2405. cursor_set_line(at);
  2406. _insert_text(at, 0, p_text+"\n");
  2407. }
  2408. void TextEdit::set_show_line_numbers(bool p_show) {
  2409. line_numbers=p_show;
  2410. update();
  2411. }
  2412. void TextEdit::_bind_methods() {
  2413. ObjectTypeDB::bind_method(_MD("_input_event"),&TextEdit::_input_event);
  2414. ObjectTypeDB::bind_method(_MD("_scroll_moved"),&TextEdit::_scroll_moved);
  2415. ObjectTypeDB::bind_method(_MD("_cursor_changed_emit"),&TextEdit::_cursor_changed_emit);
  2416. ObjectTypeDB::bind_method(_MD("_text_changed_emit"),&TextEdit::_text_changed_emit);
  2417. ObjectTypeDB::bind_method(_MD("_push_current_op"),&TextEdit::_push_current_op);
  2418. BIND_CONSTANT( SEARCH_MATCH_CASE );
  2419. BIND_CONSTANT( SEARCH_WHOLE_WORDS );
  2420. BIND_CONSTANT( SEARCH_BACKWARDS );
  2421. /*
  2422. ObjectTypeDB::bind_method(_MD("delete_char"),&TextEdit::delete_char);
  2423. ObjectTypeDB::bind_method(_MD("delete_line"),&TextEdit::delete_line);
  2424. */
  2425. ObjectTypeDB::bind_method(_MD("set_text","text"),&TextEdit::set_text);
  2426. ObjectTypeDB::bind_method(_MD("insert_text_at_cursor","text"),&TextEdit::insert_text_at_cursor);
  2427. ObjectTypeDB::bind_method(_MD("get_line_count"),&TextEdit::get_line_count);
  2428. ObjectTypeDB::bind_method(_MD("get_text"),&TextEdit::get_text);
  2429. ObjectTypeDB::bind_method(_MD("get_line"),&TextEdit::get_line);
  2430. ObjectTypeDB::bind_method(_MD("cursor_set_column","column"),&TextEdit::cursor_set_column);
  2431. ObjectTypeDB::bind_method(_MD("cursor_set_line","line"),&TextEdit::cursor_set_line);
  2432. ObjectTypeDB::bind_method(_MD("cursor_get_column"),&TextEdit::cursor_get_column);
  2433. ObjectTypeDB::bind_method(_MD("cursor_get_line"),&TextEdit::cursor_get_line);
  2434. ObjectTypeDB::bind_method(_MD("set_readonly","enable"),&TextEdit::set_readonly);
  2435. ObjectTypeDB::bind_method(_MD("set_wrap","enable"),&TextEdit::set_wrap);
  2436. ObjectTypeDB::bind_method(_MD("set_max_chars","amount"),&TextEdit::set_max_chars);
  2437. ObjectTypeDB::bind_method(_MD("cut"),&TextEdit::cut);
  2438. ObjectTypeDB::bind_method(_MD("copy"),&TextEdit::copy);
  2439. ObjectTypeDB::bind_method(_MD("paste"),&TextEdit::paste);
  2440. ObjectTypeDB::bind_method(_MD("select_all"),&TextEdit::select_all);
  2441. ObjectTypeDB::bind_method(_MD("select","from_line","from_column","to_line","to_column"),&TextEdit::select);
  2442. ObjectTypeDB::bind_method(_MD("is_selection_active"),&TextEdit::is_selection_active);
  2443. ObjectTypeDB::bind_method(_MD("get_selection_from_line"),&TextEdit::get_selection_from_line);
  2444. ObjectTypeDB::bind_method(_MD("get_selection_from_column"),&TextEdit::get_selection_from_column);
  2445. ObjectTypeDB::bind_method(_MD("get_selection_to_line"),&TextEdit::get_selection_to_line);
  2446. ObjectTypeDB::bind_method(_MD("get_selection_to_column"),&TextEdit::get_selection_to_column);
  2447. ObjectTypeDB::bind_method(_MD("get_selection_text"),&TextEdit::get_selection_text);
  2448. ObjectTypeDB::bind_method(_MD("get_word_under_cursor"),&TextEdit::get_word_under_cursor);
  2449. ObjectTypeDB::bind_method(_MD("search","flags","from_line","from_column","to_line","to_column"),&TextEdit::_search_bind);
  2450. ObjectTypeDB::bind_method(_MD("undo"),&TextEdit::undo);
  2451. ObjectTypeDB::bind_method(_MD("redo"),&TextEdit::redo);
  2452. ObjectTypeDB::bind_method(_MD("clear_undo_history"),&TextEdit::clear_undo_history);
  2453. ObjectTypeDB::bind_method(_MD("set_syntax_coloring","enable"),&TextEdit::set_syntax_coloring);
  2454. ObjectTypeDB::bind_method(_MD("is_syntax_coloring_enabled"),&TextEdit::is_syntax_coloring_enabled);
  2455. ObjectTypeDB::bind_method(_MD("add_keyword_color","keyword","color"),&TextEdit::add_keyword_color);
  2456. ObjectTypeDB::bind_method(_MD("add_color_region","begin_key","end_key","color","line_only"),&TextEdit::add_color_region,DEFVAL(false));
  2457. ObjectTypeDB::bind_method(_MD("set_symbol_color","color"),&TextEdit::set_symbol_color);
  2458. ObjectTypeDB::bind_method(_MD("set_custom_bg_color","color"),&TextEdit::set_custom_bg_color);
  2459. ObjectTypeDB::bind_method(_MD("clear_colors"),&TextEdit::clear_colors);
  2460. ADD_SIGNAL(MethodInfo("cursor_changed"));
  2461. ADD_SIGNAL(MethodInfo("text_changed"));
  2462. ADD_SIGNAL(MethodInfo("request_completion"));
  2463. }
  2464. TextEdit::TextEdit() {
  2465. readonly=false;
  2466. setting_row=false;
  2467. draw_tabs=false;
  2468. max_chars=0;
  2469. clear();
  2470. wrap=false;
  2471. set_focus_mode(FOCUS_ALL);
  2472. _update_caches();
  2473. cache.size=Size2(1,1);
  2474. tab_size=4;
  2475. text.set_tab_size(tab_size);
  2476. text.clear();
  2477. // text.insert(1,"Mongolia..");
  2478. // text.insert(2,"PAIS GENEROSO!!");
  2479. text.set_color_regions(&color_regions);
  2480. h_scroll = memnew( HScrollBar );
  2481. v_scroll = memnew( VScrollBar );
  2482. add_child(h_scroll);
  2483. add_child(v_scroll);
  2484. updating_scrolls=false;
  2485. selection.active=false;
  2486. h_scroll->connect("value_changed", this,"_scroll_moved");
  2487. v_scroll->connect("value_changed", this,"_scroll_moved");
  2488. cursor_changed_dirty=false;
  2489. text_changed_dirty=false;
  2490. selection.selecting_mode=Selection::MODE_NONE;
  2491. selection.selecting_line=0;
  2492. selection.selecting_column=0;
  2493. selection.selecting_test=false;
  2494. selection.active=false;
  2495. syntax_coloring=false;
  2496. custom_bg_color=Color(0,0,0,0);
  2497. idle_detect = memnew( Timer );
  2498. add_child(idle_detect);
  2499. idle_detect->set_one_shot(true);
  2500. idle_detect->set_wait_time(GLOBAL_DEF("display/text_edit_idle_detect_sec",3));
  2501. idle_detect->connect("timeout", this,"_push_current_op");
  2502. #if 0
  2503. syntax_coloring=true;
  2504. keywords["void"]=Color(0.3,0.0,0.1);
  2505. keywords["int"]=Color(0.3,0.0,0.1);
  2506. keywords["function"]=Color(0.3,0.0,0.1);
  2507. keywords["class"]=Color(0.3,0.0,0.1);
  2508. keywords["extends"]=Color(0.3,0.0,0.1);
  2509. keywords["constructor"]=Color(0.3,0.0,0.1);
  2510. symbol_color=Color(0.1,0.0,0.3,1.0);
  2511. color_regions.push_back(ColorRegion("/*","*/",Color(0.4,0.6,0,4)));
  2512. color_regions.push_back(ColorRegion("//","",Color(0.6,0.6,0.4)));
  2513. color_regions.push_back(ColorRegion("\"","\"",Color(0.4,0.7,0.7)));
  2514. color_regions.push_back(ColorRegion("'","'",Color(0.4,0.8,0.8)));
  2515. color_regions.push_back(ColorRegion("#","",Color(0.2,1.0,0.2)));
  2516. #endif
  2517. current_op.type=TextOperation::TYPE_NONE;
  2518. undo_enabled=true;
  2519. undo_stack_pos=NULL;
  2520. setting_text=false;
  2521. last_dblclk=0;
  2522. current_op.version=0;
  2523. version=0;
  2524. saved_version=0;
  2525. completion_enabled=false;
  2526. completion_active=false;
  2527. completion_line_ofs=0;
  2528. tooltip_obj=NULL;
  2529. line_numbers=false;
  2530. next_operation_is_complex=false;
  2531. auto_brace_completion_enabled=false;
  2532. brace_matching_enabled=false;
  2533. }
  2534. TextEdit::~TextEdit()
  2535. {
  2536. }