PolyUITextInput.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyUITextInput.h"
  20. #include "PolyConfig.h"
  21. #include "PolyInputEvent.h"
  22. #include "PolyLabel.h"
  23. #include "PolyCoreServices.h"
  24. #include "PolyEventHandler.h"
  25. using namespace Polycode;
  26. UITextInput::UITextInput(bool multiLine, Number width, Number height) : UIElement() {
  27. this->multiLine = multiLine;
  28. processInputEvents = true;
  29. isNumberOnly = false;
  30. decoratorOffset = 0;
  31. lineOffset = -1;
  32. useStrongHinting = false;
  33. draggingSelection = false;
  34. hasSelection = false;
  35. doSelectToCaret = false;
  36. lineNumbersEnabled = false;
  37. caretPosition = 0;
  38. caretImagePosition = 0;
  39. settingText = false;
  40. needFullRedraw = false;
  41. numLines = 0;
  42. this->positionMode = ScreenEntity::POSITION_TOPLEFT;
  43. Config *conf = CoreServices::getInstance()->getConfig();
  44. if(multiLine)
  45. fontName = conf->getStringValue("Polycode", "uiTextInputFontNameMultiLine");
  46. else
  47. fontName = conf->getStringValue("Polycode", "uiTextInputFontName");
  48. if(multiLine)
  49. fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSizeMultiline");
  50. else
  51. fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSize");
  52. Number rectHeight = height;
  53. if(!multiLine) {
  54. rectHeight = fontSize+12;
  55. }
  56. linesContainer = new ScreenEntity();
  57. linesContainer->processInputEvents = true;
  58. lineSpacing = conf->getNumericValue("Polycode", "textEditLineSpacing");
  59. st = conf->getNumericValue("Polycode", "textBgSkinT");
  60. sr = conf->getNumericValue("Polycode", "textBgSkinR");
  61. sb = conf->getNumericValue("Polycode", "textBgSkinB");
  62. sl = conf->getNumericValue("Polycode", "textBgSkinL");
  63. padding = conf->getNumericValue("Polycode", "textBgSkinPadding");
  64. if(multiLine) {
  65. inputRect = new UIBox(conf->getStringValue("Polycode", "textBgSkinMultiline"),
  66. st,sr,sb,sl,
  67. width+(padding*2), height+(padding*2));
  68. } else {
  69. inputRect = new UIBox(conf->getStringValue("Polycode", "textBgSkin"),
  70. st,sr,sb,sl,
  71. width+(padding*2), height+(padding*2));
  72. }
  73. addChild(inputRect);
  74. if(multiLine) {
  75. lineNumberBg = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  76. lineNumberBg->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  77. lineNumberBg->setColor(0.0, 0.0, 0.0, 0.3);
  78. addChild(lineNumberBg);
  79. lineNumberBg->visible = false;
  80. lineNumberAnchor = new ScreenEntity();
  81. linesContainer->addChild(lineNumberAnchor);
  82. } else {
  83. lineNumberBg = NULL;
  84. lineNumberAnchor = NULL;
  85. decoratorOffset = sl/2.0;
  86. }
  87. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  88. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  89. inputRect->addEventListener(this, InputEvent::EVENT_DOUBLECLICK);
  90. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  91. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
  92. inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
  93. inputRect->processInputEvents = true;
  94. inputRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  95. selectorRectTop = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  96. selectorRectTop->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  97. selectorRectTop->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  98. selectorRectTop->visible = false;
  99. linesContainer->addChild(selectorRectTop);
  100. selectorRectMiddle = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  101. selectorRectMiddle->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  102. selectorRectMiddle->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  103. selectorRectMiddle->visible = false;
  104. linesContainer->addChild(selectorRectMiddle);
  105. selectorRectBottom = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
  106. selectorRectBottom->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  107. selectorRectBottom->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
  108. selectorRectBottom->visible = false;
  109. linesContainer->addChild(selectorRectBottom);
  110. blinkerRect = new ScreenShape(ScreenShape::SHAPE_RECT, 1, fontSize+2,0,0);
  111. blinkerRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  112. blinkerRect->setColor(0,0,0,1);
  113. linesContainer->addChild(blinkerRect);
  114. blinkerRect->visible = false;
  115. blinkerRect->setPosition(0,3);
  116. blinkTimer = new Timer(true, 500);
  117. blinkTimer->addEventListener(this, Timer::EVENT_TRIGGER);
  118. focusable = true;
  119. this->width = width;
  120. this->height = rectHeight;
  121. setHitbox(width, rectHeight);
  122. updateCaretPosition();
  123. scrollContainer = NULL;
  124. if(multiLine) {
  125. scrollContainer = new UIScrollContainer(linesContainer, false, true, 200, 200);
  126. scrollContainer->addEventListener(this, Event::CHANGE_EVENT);
  127. addChild(scrollContainer);
  128. } else {
  129. addChild(linesContainer);
  130. enableScissor = true;
  131. }
  132. undoStateIndex = 0;
  133. maxRedoIndex = 0;
  134. syntaxHighliter = NULL;
  135. textColor = Color(0.0,0.0,0.0,1.0);
  136. currentBufferLines = 0;
  137. neededBufferLines = 1;
  138. checkBufferLines();
  139. insertLine(true);
  140. }
  141. void UITextInput::checkBufferLines() {
  142. if(neededBufferLines < currentBufferLines)
  143. return;
  144. int aaMode = Label::ANTIALIAS_FULL;
  145. if(useStrongHinting) {
  146. aaMode = Label::ANTIALIAS_STRONG;
  147. }
  148. for(int i=0; i < neededBufferLines - currentBufferLines; i++) {
  149. if(multiLine) {
  150. ScreenLabel *newNumberLine = new ScreenLabel(L"", fontSize, fontName, aaMode);
  151. newNumberLine->color = lineNumberColor;
  152. lineNumberAnchor->addChild(newNumberLine);
  153. numberLines.push_back(newNumberLine);
  154. if(!lineNumbersEnabled) {
  155. newNumberLine->visible = false;
  156. }
  157. }
  158. ScreenLabel *newLine = new ScreenLabel(L"", fontSize, fontName, aaMode);
  159. newLine->color = textColor;
  160. lineHeight = newLine->getHeight();
  161. linesContainer->addChild(newLine);
  162. bufferLines.push_back(newLine);
  163. }
  164. currentBufferLines = neededBufferLines;
  165. }
  166. void UITextInput::setNumberOnly(bool val) {
  167. isNumberOnly = val;
  168. }
  169. void UITextInput::clearSelection() {
  170. hasSelection = false;
  171. selectorRectTop->visible = false;
  172. selectorRectMiddle->visible = false;
  173. selectorRectBottom->visible = false;
  174. }
  175. void UITextInput::setSelection(int lineStart, int lineEnd, int colStart, int colEnd) {
  176. if(lineStart == lineEnd && colStart == colEnd) {
  177. clearSelection();
  178. return;
  179. }
  180. if(lineStart == lineOffset) {
  181. selectionLine = lineEnd;
  182. } else {
  183. selectionLine = lineStart;
  184. }
  185. if(colStart == caretPosition) {
  186. selectionCaretPosition = colEnd;
  187. } else {
  188. selectionCaretPosition = colStart;
  189. }
  190. // printf("SET lineStart:%d lineEnd:%d colStart:%d colEnd:%d\n", lineStart, lineEnd, colStart, colEnd);
  191. if(lineStart > lineEnd) {
  192. int tmp = lineStart;
  193. lineStart = lineEnd;
  194. lineEnd = tmp;
  195. tmp = colStart;
  196. colStart = colEnd;
  197. colEnd = tmp;
  198. }
  199. if(colStart > colEnd && lineStart == lineEnd) {
  200. int tmp = colStart;
  201. colStart = colEnd;
  202. colEnd = tmp;
  203. }
  204. clearSelection();
  205. if(lineStart > lines.size()-1)
  206. return;
  207. String topLine = lines[lineStart];
  208. if(colStart+1 > topLine.length()) {
  209. colStart = topLine.length();
  210. }
  211. Number fColEnd = colEnd;
  212. if(colEnd > topLine.length() || lineStart != lineEnd)
  213. fColEnd = topLine.length();
  214. Number topSize, topHeight, topX;
  215. selectorRectTop->visible = true;
  216. topSize = bufferLines[0]->getLabel()->getTextWidthForString(topLine.substr(colStart,fColEnd-colStart)) ;
  217. topHeight = lineHeight+lineSpacing;
  218. if(colStart >= 0) {
  219. topX = bufferLines[0]->getLabel()->getTextWidthForString(topLine.substr(0,colStart)) + 2;
  220. } else {
  221. topX = 0;
  222. }
  223. selectorRectTop->setScale(topSize, topHeight);
  224. selectorRectTop->setPosition(decoratorOffset + topX + padding + (topSize/2.0), padding + (lineStart * (lineHeight+lineSpacing)) + (topHeight/2.0));
  225. if(lineEnd > lineStart && lineEnd < lines.size()) {
  226. String bottomLine = lines[lineEnd];
  227. selectorRectBottom->visible = true;
  228. Number bottomSize = bufferLines[0]->getLabel()->getTextWidthForString(bottomLine.substr(0,colEnd)) ;
  229. if(bottomSize < 0)
  230. bottomSize = this->width-padding;
  231. Number bottomHeight = lineHeight+lineSpacing;
  232. selectorRectBottom->setScale(bottomSize, bottomHeight);
  233. selectorRectBottom->setPosition(decoratorOffset + padding + (bottomSize/2.0), padding + (lineEnd * (lineHeight+lineSpacing)) + (bottomHeight/2.0));
  234. if(lineEnd != lineStart+1) {
  235. // need filler
  236. selectorRectMiddle->visible = true;
  237. Number midSize = this->width-padding;
  238. Number midHeight = 0;
  239. for(int i=lineStart+1; i < lineEnd;i++) {
  240. midHeight += lineHeight+lineSpacing;
  241. }
  242. selectorRectMiddle->setScale(midSize, midHeight);
  243. selectorRectMiddle->setPosition(decoratorOffset + padding + (midSize/2.0), padding + ((lineStart+1) * (lineHeight+lineSpacing)) + (midHeight/2.0));
  244. }
  245. }
  246. hasSelection = true;
  247. selectionTop = lineStart;
  248. selectionBottom = lineEnd;
  249. selectionL = colStart;
  250. selectionR = colEnd;
  251. }
  252. void UITextInput::deleteSelection() {
  253. if(selectionTop == selectionBottom) {
  254. String ctext = lines[selectionTop];
  255. String newText = ctext.substr(0, selectionL);
  256. int rside = selectionR;
  257. if(rside > ctext.length()-1)
  258. rside = ctext.length() - 1;
  259. newText += ctext.substr(rside,ctext.length() - selectionR);
  260. lines[selectionTop] = newText;
  261. } else {
  262. String ctext = lines[selectionTop];
  263. String newText = ctext.substr(0, selectionL);
  264. lines[selectionTop] = newText;
  265. ctext = lines[selectionBottom];
  266. int rside = selectionR;
  267. if(rside > ctext.length()-1)
  268. rside = ctext.length() - 1;
  269. newText = ctext.substr(rside,ctext.length() - selectionR);
  270. lineOffset = selectionTop;
  271. selectLineFromOffset();
  272. caretPosition = lines[lineOffset].length();
  273. updateCaretPosition();
  274. lines[lineOffset] = lines[lineOffset] + newText;
  275. removeLines(selectionTop+1, selectionBottom+1);
  276. }
  277. clearSelection();
  278. caretPosition = selectionL;
  279. updateCaretPosition();
  280. changedText();
  281. }
  282. void UITextInput::applySyntaxFormatting() {
  283. if(syntaxHighliter && multiLine) {
  284. int startLine = (-linesContainer->getPosition().y) / (lineHeight+lineSpacing);
  285. unsigned int endLine = startLine + ((int)((height / (lineHeight+lineSpacing)))) + 1;
  286. if(startLine < 0)
  287. startLine = 0;
  288. if(endLine > lines.size())
  289. endLine = lines.size();
  290. if(needFullRedraw) {
  291. startLine = 0;
  292. endLine = lines.size();
  293. }
  294. String totalText = L"";
  295. for(int i=startLine; i < endLine; i++) {
  296. totalText += lines[i];
  297. if(i < lines.size()-1)
  298. totalText += L"\n";
  299. }
  300. std::vector<SyntaxHighlightToken> tokens = syntaxHighliter->parseText(totalText);
  301. // DO SYNTAX HIGHLIGHTING
  302. if(needFullRedraw) {
  303. lineColors.clear();
  304. for(int i=0; i < lines.size(); i++) {
  305. lineColors.push_back(LineColorInfo());
  306. }
  307. needFullRedraw = false;
  308. } else {
  309. std::vector<LineColorInfo> newInfo;
  310. for(int i=0; i < lines.size(); i++) {
  311. if((i >= startLine && i < endLine) || i >= lineColors.size()) {
  312. newInfo.push_back(LineColorInfo());
  313. } else {
  314. newInfo.push_back(lineColors[i]);
  315. }
  316. }
  317. lineColors = newInfo;
  318. }
  319. int lineIndex = startLine;
  320. int rangeStart = 0;
  321. int rangeEnd = 0;
  322. for(int i=0; i < tokens.size(); i++) {
  323. if(tokens[i].text == "\n") {
  324. lineIndex++;
  325. if(lineIndex >= endLine) {
  326. lineIndex = endLine-1;
  327. }
  328. rangeStart = 0;
  329. rangeEnd = 0;
  330. } else {
  331. if(lineIndex < lines.size()) {
  332. int textLength = tokens[i].text.length();
  333. if(tokens[i].text.length() > 1) {
  334. rangeEnd = rangeStart + textLength-1;
  335. lineColors[lineIndex].colors.push_back(LineColorData(tokens[i].color, rangeStart, rangeEnd));
  336. rangeStart = rangeStart + textLength;
  337. } else {
  338. rangeEnd = rangeStart;
  339. lineColors[lineIndex].colors.push_back(LineColorData(tokens[i].color, rangeStart, rangeEnd));
  340. rangeStart++;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. readjustBuffer();
  347. }
  348. void UITextInput::changedText() {
  349. if(settingText)
  350. return;
  351. applySyntaxFormatting();
  352. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  353. }
  354. void UITextInput::setSyntaxHighlighter(UITextInputSyntaxHighlighter *syntaxHighliter) {
  355. this->syntaxHighliter = syntaxHighliter;
  356. }
  357. void UITextInput::Resize(Number width, Number height) {
  358. inputRect->resizeBox(width, height);
  359. this->width = width;
  360. this->height = height;
  361. matrixDirty = true;
  362. setHitbox(width,height);
  363. if(multiLine) {
  364. inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
  365. neededBufferLines = (height / ( lineHeight+lineSpacing)) + 1;
  366. checkBufferLines();
  367. renumberLines();
  368. applySyntaxFormatting();
  369. }
  370. if(multiLine && lineNumbersEnabled) {
  371. lineNumberBg->setShapeSize(decoratorOffset, height);
  372. }
  373. if(scrollContainer) {
  374. scrollContainer->Resize(width, height);
  375. }
  376. }
  377. int UITextInput::insertLine(bool after) {
  378. numLines++;
  379. if(after) {
  380. String newText = "";
  381. if(lines.size() > 0) {
  382. String ctext = lines[lineOffset];
  383. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  384. ctext = ctext.substr(0,caretPosition);
  385. lines[lineOffset] = ctext;
  386. newText = text2;
  387. caretPosition=0;
  388. }
  389. vector<String>::iterator it;
  390. it = lines.begin();
  391. for(int i=0; i < lineOffset+1; i++) {
  392. it++;
  393. }
  394. lineOffset = lineOffset + 1;
  395. lines.insert(it,newText);
  396. renumberLines();
  397. restructLines();
  398. } else {
  399. // do we even need that? I don't think so.
  400. }
  401. changedText();
  402. return 1;
  403. }
  404. void UITextInput::enableLineNumbers(bool val) {
  405. lineNumbersEnabled = val;
  406. lineNumberBg->visible = lineNumbersEnabled;
  407. restructLines();
  408. }
  409. void UITextInput::renumberLines() {
  410. if(!multiLine)
  411. return;
  412. int totalLineNumber = lines.size();
  413. if(currentBufferLines > lines.size()) {
  414. totalLineNumber = currentBufferLines;
  415. }
  416. decoratorOffset = 0;
  417. if(multiLine) {
  418. if(lineNumbersEnabled) {
  419. decoratorOffset = 15;
  420. if(totalLineNumber > 9) {
  421. decoratorOffset = 25;
  422. }
  423. if(totalLineNumber > 99) {
  424. decoratorOffset = 35;
  425. }
  426. if(totalLineNumber > 999) {
  427. decoratorOffset = 45;
  428. }
  429. if(totalLineNumber > 9999) {
  430. decoratorOffset = 55;
  431. }
  432. }
  433. }
  434. lineNumberAnchor->setPositionX(padding+decoratorOffset - 10);
  435. }
  436. void UITextInput::restructLines() {
  437. for(int i=0; i < bufferLines.size(); i++) {
  438. bufferLines[i]->setPosition(decoratorOffset + padding,padding + (i*(lineHeight+lineSpacing)),0.0f);
  439. }
  440. if(multiLine && lineNumbersEnabled) {
  441. lineNumberBg->setShapeSize(decoratorOffset, height);
  442. }
  443. if(scrollContainer) {
  444. scrollContainer->setContentSize(width, (((lines.size()) * ((lineHeight+lineSpacing)))) + padding);
  445. }
  446. if(multiLine) {
  447. inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
  448. }
  449. }
  450. void UITextInput::setText(String text) {
  451. if(!multiLine) {
  452. lines[lineOffset] = text;
  453. caretPosition = text.length();
  454. clearSelection();
  455. updateCaretPosition();
  456. } else {
  457. selectAll();
  458. insertText(text);
  459. clearSelection();
  460. }
  461. // needFullRedraw = true;
  462. changedText();
  463. }
  464. void UITextInput::onLoseFocus() {
  465. blinkerRect->visible = false;
  466. clearSelection();
  467. }
  468. String UITextInput::getText() {
  469. if(!multiLine) {
  470. return lines[0];
  471. } else {
  472. String totalText = L"";
  473. for(int i=0; i < lines.size(); i++) {
  474. totalText += lines[i];
  475. if(i < lines.size()-1)
  476. totalText += L"\n";
  477. }
  478. return totalText;
  479. }
  480. }
  481. void UITextInput::updateCaretPosition() {
  482. caretImagePosition = padding;
  483. if(caretPosition == 0) {
  484. caretImagePosition = padding;
  485. } else if(caretPosition > lines[lineOffset].length()) {
  486. caretPosition = lines[lineOffset].length();
  487. String caretSubString = lines[lineOffset].substr(0,caretPosition);
  488. caretImagePosition = bufferLines[0]->getLabel()->getTextWidthForString(caretSubString);
  489. caretImagePosition = caretImagePosition + padding;
  490. } else {
  491. String caretSubString = lines[lineOffset].substr(0,caretPosition);
  492. caretImagePosition = bufferLines[0]->getLabel()->getTextWidthForString(caretSubString);
  493. caretImagePosition = caretImagePosition + padding;
  494. }
  495. blinkerRect->visible = true;
  496. blinkTimer->Reset();
  497. if(doSelectToCaret) {
  498. doSelectToCaret = false;
  499. }
  500. /*
  501. if(multiLine) {
  502. if(linesContainer->getPosition().y + currentLine->getPosition2D().y < 0.0) {
  503. scrollContainer->scrollVertical(-(lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  504. } else if(linesContainer->getPosition().y + currentLine->getPosition2D().y > scrollContainer->getHeight()) {
  505. scrollContainer->scrollVertical((lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  506. }
  507. }
  508. */
  509. }
  510. void UITextInput::selectLineFromOffset() {
  511. lineOffset = lineOffset;
  512. }
  513. void UITextInput::dragSelectionTo(Number x, Number y) {
  514. x -= (padding * 2.0) + decoratorOffset;
  515. y -= padding;
  516. int lineOffset = y / (lineHeight+lineSpacing);
  517. if(lineOffset > lines.size()-1)
  518. lineOffset = lines.size()-1;
  519. String selectToLine = lines[lineOffset];
  520. int len = selectToLine.length();
  521. Number slen;
  522. int caretPosition = bufferLines[0]->getLabel()->getTextWidthForString(selectToLine.substr(0,len));
  523. for(int i=0; i < len; i++) {
  524. slen = bufferLines[0]->getLabel()->getTextWidthForString(selectToLine.substr(0,i));
  525. if(slen > x) {
  526. caretPosition = i;
  527. break;
  528. }
  529. }
  530. if(x > slen)
  531. caretPosition = len;
  532. // if(multiLine)
  533. // caretPosition++;
  534. if(caretPosition < 0)
  535. caretPosition = 0;
  536. setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
  537. }
  538. int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
  539. for(int i=caretPosition; i > 0; i--) {
  540. String bit = lines[caretLine].substr(i,1);
  541. char chr = ((char*)bit.c_str())[0];
  542. if(!isNumberOrCharacter(chr) && i < caretPosition-1) {
  543. return i+1;
  544. }
  545. }
  546. return 0;
  547. }
  548. int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
  549. int len = lines[caretLine].length();
  550. for(int i=caretPosition; i < len; i++) {
  551. String bit = lines[caretLine].substr(i,1);
  552. char chr = ((char*)bit.c_str())[0];
  553. if(!isNumberOrCharacter(chr) && i > caretPosition) {
  554. return i;
  555. }
  556. }
  557. return lines[caretLine].length();
  558. }
  559. void UITextInput::selectWordAtCaret() {
  560. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  561. clearSelection();
  562. updateCaretPosition();
  563. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  564. updateCaretPosition();
  565. }
  566. void UITextInput::replaceAll(String what, String withWhat) {
  567. for(int i=0; i < lines.size(); i++) {
  568. lines[i] = lines[i].replace(what, withWhat);
  569. }
  570. needFullRedraw = true;
  571. changedText();
  572. }
  573. void UITextInput::findString(String stringToFind, bool replace, String replaceString) {
  574. clearSelection();
  575. findMatches.clear();
  576. for(int i=0; i < lines.size(); i++) {
  577. String lineText = lines[i];
  578. int offset = 0;
  579. int retVal = -1;
  580. do {
  581. retVal = lineText.find(stringToFind, offset);
  582. if(retVal != -1) {
  583. FindMatch match;
  584. match.lineNumber = i;
  585. match.caretStart = retVal;
  586. match.caretEnd = retVal + stringToFind.length();
  587. findMatches.push_back(match);
  588. offset = retVal + stringToFind.length();
  589. }
  590. } while(retVal != -1);
  591. }
  592. if(findMatches.size() > 0) {
  593. if(replace) {
  594. FindMatch match = findMatches[findIndex];
  595. String oldText = lines[match.lineNumber];
  596. String newText = oldText.substr(0,match.caretStart) + replaceString + oldText.substr(match.caretEnd);
  597. lines[match.lineNumber] = newText;
  598. findMatches[findIndex].caretEnd = findMatches[findIndex].caretStart + replaceString.length();
  599. changedText();
  600. }
  601. findIndex = 0;
  602. findCurrent();
  603. }
  604. }
  605. void UITextInput::findNext() {
  606. if(findMatches.size() == 0)
  607. return;
  608. findIndex++;
  609. if(findIndex == findMatches.size()) {
  610. findIndex = 0;
  611. }
  612. findCurrent();
  613. }
  614. void UITextInput::findPrevious() {
  615. if(findMatches.size() == 0)
  616. return;
  617. findIndex--;
  618. if(findIndex < 0) {
  619. findIndex = findMatches.size()-1;
  620. }
  621. findCurrent();
  622. }
  623. void UITextInput::findCurrent() {
  624. if(findMatches.size() == 0)
  625. return;
  626. FindMatch match = findMatches[findIndex];
  627. lineOffset = match.lineNumber;
  628. caretPosition = match.caretStart;
  629. lineOffset = match.lineNumber;
  630. updateCaretPosition();
  631. showLine(findMatches[findIndex].lineNumber, false);
  632. setSelection(match.lineNumber, match.lineNumber, match.caretStart, match.caretEnd);
  633. }
  634. void UITextInput::setCaretToMouse(Number x, Number y) {
  635. clearSelection();
  636. x -= (padding) + decoratorOffset;
  637. y -= padding;
  638. //if(lines.size() > 1) {
  639. lineOffset = y / (lineHeight+lineSpacing);
  640. if(lineOffset > lines.size()-1)
  641. lineOffset = lines.size()-1;
  642. selectLineFromOffset();
  643. //}
  644. int len = lines[lineOffset].length();
  645. Number slen;
  646. int newCaretPosition = -1;
  647. for(int i=1; i < len; i++) {
  648. slen = bufferLines[0]->getLabel()->getTextWidthForString(lines[lineOffset].substr(0,i));
  649. Number slen_prev = bufferLines[0]->getLabel()->getTextWidthForString(lines[lineOffset].substr(0,i-1));
  650. if(x >= slen_prev && x <= slen) {
  651. if(x < slen_prev + ((slen - slen_prev) /2.0)) {
  652. newCaretPosition = i-1;
  653. break;
  654. } else {
  655. newCaretPosition = i;
  656. break;
  657. }
  658. }
  659. }
  660. if(newCaretPosition == -1)
  661. newCaretPosition = 0;
  662. if(x > slen)
  663. newCaretPosition = len;
  664. caretPosition = newCaretPosition;
  665. updateCaretPosition();
  666. }
  667. void UITextInput::removeLines(unsigned int startIndex, unsigned int endIndex) {
  668. lines.erase(lines.begin()+startIndex, lines.begin()+endIndex);
  669. renumberLines();
  670. restructLines();
  671. changedText();
  672. }
  673. void UITextInput::selectAll() {
  674. setSelection(0, lines.size()-1, 0, lines[lines.size()-1].length());
  675. }
  676. void UITextInput::insertText(String text) {
  677. vector<String> strings = text.split("\n");
  678. settingText = true;
  679. if(hasSelection)
  680. deleteSelection();
  681. if(strings.size() > 1) {
  682. String ctext = lines[lineOffset];
  683. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  684. ctext = ctext.substr(0,caretPosition);
  685. ctext += strings[0];
  686. lines[lineOffset] = ctext;
  687. caretPosition = ctext.length();
  688. for(int i=1; i < strings.size()-1; i++) {
  689. insertLine(true);
  690. ctext = strings[i];
  691. lines[lineOffset] = ctext;
  692. caretPosition = ctext.length();
  693. }
  694. insertLine(true);
  695. ctext = strings[strings.size()-1] + text2;
  696. caretPosition = ctext.length();
  697. lines[lineOffset] = ctext;
  698. } else {
  699. String ctext = lines[lineOffset];
  700. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  701. ctext = ctext.substr(0,caretPosition);
  702. ctext += text + text2;
  703. caretPosition += text.length();
  704. lines[lineOffset] = ctext;
  705. }
  706. settingText = false;
  707. restructLines();
  708. renumberLines();
  709. changedText();
  710. updateCaretPosition();
  711. }
  712. String UITextInput::getLineText(unsigned int index) {
  713. if(index < lines.size()) {
  714. return lines[index];
  715. } else {
  716. return "";
  717. }
  718. }
  719. String UITextInput::getSelectionText() {
  720. if(!hasSelection)
  721. return L"";
  722. String totalText = L"";
  723. if(selectionTop == selectionBottom) {
  724. totalText = lines[selectionTop].substr(selectionL, selectionR-selectionL);
  725. return totalText;
  726. } else {
  727. totalText += lines[selectionTop].substr(selectionL, lines[selectionTop].length()-selectionL);
  728. totalText += L"\n";
  729. }
  730. if(selectionBottom > selectionTop+1) {
  731. for(int i=selectionTop+1; i <= selectionBottom; i++) {
  732. totalText += lines[i];
  733. totalText += L"\n";
  734. }
  735. }
  736. totalText += lines[selectionBottom].substr(0, selectionL);
  737. return totalText;
  738. }
  739. void UITextInput::setSelectionColor(Color color) {
  740. selectorRectTop->color = color;
  741. selectorRectMiddle->color = color;
  742. selectorRectBottom->color = color;
  743. }
  744. void UITextInput::setCursorColor(Color color) {
  745. blinkerRect->color = color;
  746. }
  747. void UITextInput::setBackgroundColor(Color color) {
  748. inputRect->color = color;
  749. }
  750. void UITextInput::setLineNumberColor(Color color) {
  751. lineNumberColor = color;
  752. for(int i=0; i < numberLines.size(); i++) {
  753. numberLines[i]->color = lineNumberColor;
  754. }
  755. }
  756. void UITextInput::setTextColor(Color color) {
  757. textColor = color;
  758. for(int i=0; i < bufferLines.size(); i++) {
  759. bufferLines[i]->color = textColor;
  760. }
  761. }
  762. UIScrollContainer *UITextInput::getScrollContainer() {
  763. return scrollContainer;
  764. }
  765. void UITextInput::saveUndoState() {
  766. UITextInputUndoState newState;
  767. newState.content = getText();
  768. newState.caretPosition = caretPosition;
  769. newState.lineOffset = lineOffset;
  770. newState.hasSelection = hasSelection;
  771. if(hasSelection) {
  772. newState.selectionLine = selectionLine;
  773. newState.selectionCaretPosition = selectionCaretPosition;
  774. }
  775. undoStates[undoStateIndex] = newState;
  776. // if we hit undo state capacity, shift the whole stack
  777. if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
  778. for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
  779. undoStates[i] = undoStates[i+1];
  780. }
  781. } else {
  782. undoStateIndex++;
  783. }
  784. maxRedoIndex = undoStateIndex;
  785. }
  786. void UITextInput::setUndoState(UITextInputUndoState state) {
  787. clearSelection();
  788. setText(state.content);
  789. lineOffset = state.lineOffset;
  790. caretPosition = state.caretPosition;
  791. lineOffset = state.lineOffset;
  792. updateCaretPosition();
  793. if(state.hasSelection) {
  794. setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
  795. }
  796. }
  797. void UITextInput::Undo() {
  798. if(undoStateIndex > 0) {
  799. undoStateIndex--;
  800. setUndoState(undoStates[undoStateIndex]);
  801. }
  802. }
  803. void UITextInput::Redo() {
  804. if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
  805. undoStateIndex++;
  806. setUndoState(undoStates[undoStateIndex]);
  807. }
  808. }
  809. void UITextInput::Cut() {
  810. saveUndoState();
  811. Copy();
  812. if(hasSelection) {
  813. deleteSelection();
  814. }
  815. }
  816. void UITextInput::Copy() {
  817. if(hasSelection) {
  818. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  819. }
  820. }
  821. void UITextInput::Paste() {
  822. saveUndoState();
  823. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  824. }
  825. void UITextInput::showLine(unsigned int lineNumber, bool top) {
  826. if(top) {
  827. scrollContainer->setScrollValue(0.0, ((((lineNumber) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
  828. } else {
  829. scrollContainer->setScrollValue(0.0, (((((lineNumber) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()/2.0))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
  830. }
  831. }
  832. bool UITextInput::isNumberOrCharacter(wchar_t charCode) {
  833. if(charCode > 47 && charCode < 58)
  834. return true;
  835. if(charCode > 64 && charCode < 91)
  836. return true;
  837. if(charCode > 96 && charCode < 123)
  838. return true;
  839. return false;
  840. }
  841. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  842. if(!hasFocus)
  843. return;
  844. // Logger::log("UCHAR: %d\n", charCode);
  845. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  846. if(key == KEY_a && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  847. selectAll();
  848. return;
  849. }
  850. if(key == KEY_c && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  851. Copy();
  852. return;
  853. }
  854. if(key == KEY_x && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  855. Cut();
  856. return;
  857. }
  858. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  859. Undo();
  860. return;
  861. }
  862. if(key == KEY_z && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) && (input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT))) {
  863. Redo();
  864. return;
  865. }
  866. if(key == KEY_y && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  867. Redo();
  868. return;
  869. }
  870. if(key == KEY_v && (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER))) {
  871. Paste();
  872. return;
  873. }
  874. if(key == KEY_LEFT) {
  875. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  876. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  877. if(hasSelection) {
  878. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  879. } else {
  880. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  881. }
  882. } else {
  883. caretPosition = 0;
  884. clearSelection();
  885. updateCaretPosition();
  886. }
  887. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  888. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  889. if(hasSelection) {
  890. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  891. } else {
  892. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  893. }
  894. } else {
  895. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  896. clearSelection();
  897. updateCaretPosition();
  898. }
  899. } else {
  900. if(caretPosition > 0) {
  901. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  902. if(hasSelection) {
  903. if(selectionCaretPosition > 0)
  904. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  905. } else {
  906. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition-1);
  907. }
  908. } else {
  909. caretPosition--;
  910. clearSelection();
  911. updateCaretPosition();
  912. }
  913. }
  914. }
  915. return;
  916. }
  917. if(key == KEY_RIGHT) {
  918. if(caretPosition < lines[lineOffset].length()) {
  919. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  920. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  921. if(hasSelection) {
  922. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine].length());
  923. } else {
  924. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, lines[lineOffset].length());
  925. }
  926. } else {
  927. caretPosition = lines[lineOffset].length();
  928. clearSelection();
  929. }
  930. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  931. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  932. if(hasSelection) {
  933. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  934. } else {
  935. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  936. }
  937. } else {
  938. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  939. clearSelection();
  940. }
  941. } else {
  942. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  943. if(hasSelection) {
  944. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  945. } else {
  946. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  947. }
  948. } else {
  949. caretPosition++;
  950. clearSelection();
  951. }
  952. }
  953. updateCaretPosition();
  954. }
  955. return;
  956. }
  957. if(key == KEY_PAGEUP) {
  958. if(multiLine) {
  959. scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  960. }
  961. return;
  962. }
  963. if(key == KEY_PAGEDOWN) {
  964. if(multiLine) {
  965. scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  966. }
  967. return;
  968. }
  969. if(key == KEY_UP) {
  970. if(multiLine) {
  971. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  972. if(hasSelection) {
  973. if(selectionLine > 0)
  974. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  975. } else {
  976. if(this->lineOffset > 0)
  977. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  978. }
  979. } else {
  980. clearSelection();
  981. if(lineOffset > 0) {
  982. lineOffset--;
  983. selectLineFromOffset();
  984. updateCaretPosition();
  985. }
  986. }
  987. }
  988. blinkerRect->visible = true;
  989. return;
  990. }
  991. if(key == KEY_DOWN) {
  992. if(multiLine) {
  993. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  994. if(hasSelection) {
  995. if(selectionLine < lines.size()-1)
  996. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  997. } else {
  998. if(this->lineOffset < lines.size()-1)
  999. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  1000. }
  1001. } else {
  1002. clearSelection();
  1003. if(lineOffset < lines.size()-1) {
  1004. lineOffset++;
  1005. selectLineFromOffset();
  1006. updateCaretPosition();
  1007. }
  1008. }
  1009. }
  1010. blinkerRect->visible = true;
  1011. return;
  1012. }
  1013. if(key == KEY_ESCAPE) {
  1014. if(!multiLine) {
  1015. dispatchEvent(new Event(), Event::CANCEL_EVENT);
  1016. }
  1017. }
  1018. if(key == KEY_RETURN) {
  1019. if(multiLine) {
  1020. saveUndoState();
  1021. if(hasSelection) {
  1022. deleteSelection();
  1023. }
  1024. insertLine(true);
  1025. updateCaretPosition();
  1026. } else {
  1027. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  1028. }
  1029. return;
  1030. }
  1031. String ctext = lines[lineOffset];
  1032. if((charCode > 31 && charCode < 127) || charCode > 127) {
  1033. if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
  1034. if(!isNumberOrCharacter(charCode)) {
  1035. saveUndoState();
  1036. }
  1037. if(hasSelection)
  1038. deleteSelection();
  1039. ctext = lines[lineOffset];
  1040. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1041. ctext = ctext.substr(0,caretPosition);
  1042. ctext += charCode + text2;
  1043. caretPosition++;
  1044. }
  1045. }
  1046. if(key == KEY_TAB && multiLine) {
  1047. saveUndoState();
  1048. if(hasSelection)
  1049. deleteSelection();
  1050. ctext = lines[lineOffset];
  1051. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1052. ctext = ctext.substr(0,caretPosition);
  1053. ctext += (wchar_t)'\t' + text2;
  1054. caretPosition++;
  1055. }
  1056. if(key == KEY_BACKSPACE) {
  1057. if(hasSelection) {
  1058. saveUndoState();
  1059. deleteSelection();
  1060. return;
  1061. } else {
  1062. ctext = lines[lineOffset];
  1063. if(caretPosition > 0) {
  1064. saveUndoState();
  1065. if(ctext.length() > 0) {
  1066. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1067. ctext = ctext.substr(0,caretPosition-1);
  1068. ctext += text2;
  1069. caretPosition--;
  1070. }
  1071. } else {
  1072. if(lineOffset > 0) {
  1073. saveUndoState();
  1074. lineOffset--;
  1075. selectLineFromOffset();
  1076. caretPosition = lines[lineOffset].length();
  1077. updateCaretPosition();
  1078. lines[lineOffset] = lines[lineOffset] + ctext;
  1079. removeLines(lineOffset+1, lineOffset+2);
  1080. return;
  1081. }
  1082. }
  1083. }
  1084. }
  1085. lines[lineOffset] = ctext;
  1086. changedText();
  1087. updateCaretPosition();
  1088. }
  1089. void UITextInput::Update() {
  1090. if(!multiLine) {
  1091. Vector2 pos = getScreenPosition();
  1092. scissorBox.setRect(pos.x,pos.y, width+sr+sl, height+sb+st);
  1093. }
  1094. if(hasSelection) {
  1095. blinkerRect->visible = false;
  1096. }
  1097. blinkerRect->setPosition(decoratorOffset + caretImagePosition + 1, padding + (lineOffset * ( lineHeight+lineSpacing)));
  1098. if(hasFocus) {
  1099. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  1100. } else {
  1101. blinkerRect->visible = false;
  1102. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  1103. }
  1104. for(int i=0; i < linesToDelete.size(); i++) {
  1105. delete linesToDelete[i];
  1106. }
  1107. linesToDelete.clear();
  1108. }
  1109. UITextInput::~UITextInput() {
  1110. delete linesContainer;
  1111. delete inputRect;
  1112. delete lineNumberBg;
  1113. delete lineNumberAnchor;
  1114. delete selectorRectTop;
  1115. delete selectorRectMiddle;
  1116. delete selectorRectBottom;
  1117. delete blinkerRect;
  1118. delete blinkTimer;
  1119. }
  1120. void UITextInput::readjustBuffer() {
  1121. int bufferOffset = -linesContainer->position.y/ ( lineHeight+lineSpacing);
  1122. Number bufferLineOffset = bufferOffset * ( lineHeight+lineSpacing);
  1123. for(int i=0; i < bufferLines.size(); i++) {
  1124. bufferLines[i]->getLabel()->clearColors();
  1125. if(bufferOffset + i < lines.size()) {
  1126. if(bufferOffset+i < lineColors.size()) {
  1127. for(int j=0; j < lineColors[bufferOffset+i].colors.size(); j++) {
  1128. bufferLines[i]->getLabel()->setColorForRange(lineColors[bufferOffset+i].colors[j].color, lineColors[bufferOffset+i].colors[j].rangeStart, lineColors[bufferOffset+i].colors[j].rangeEnd);
  1129. bufferLines[i]->setColor(1.0, 1.0, 1.0, 1.0);
  1130. }
  1131. }
  1132. bufferLines[i]->setText(lines[bufferOffset+i]);
  1133. } else {
  1134. bufferLines[i]->setText("");
  1135. }
  1136. bufferLines[i]->setPosition(decoratorOffset + padding,padding + bufferLineOffset + (i*(lineHeight+lineSpacing)),0.0f);
  1137. }
  1138. for(int i=0; i < numberLines.size(); i++) {
  1139. if(lineNumbersEnabled) {
  1140. numberLines[i]->setText(String::IntToString(bufferOffset+i+1));
  1141. int textWidth = ceil(numberLines[i]->getLabel()->getTextWidth());
  1142. numberLines[i]->setPosition(-textWidth,padding + bufferLineOffset + (i*(lineHeight+lineSpacing)),0.0f);
  1143. numberLines[i]->visible = true;
  1144. }
  1145. }
  1146. }
  1147. void UITextInput::handleEvent(Event *event) {
  1148. if(event->getDispatcher() == scrollContainer) {
  1149. if(event->getEventCode() == Event::CHANGE_EVENT) {
  1150. applySyntaxFormatting();
  1151. }
  1152. }
  1153. if(event->getDispatcher() == inputRect) {
  1154. switch(event->getEventCode()) {
  1155. case InputEvent::EVENT_MOUSEDOWN:
  1156. if(parentEntity) {
  1157. ((ScreenEntity*)parentEntity)->focusChild(this);
  1158. } else {
  1159. hasFocus = true;
  1160. }
  1161. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1162. draggingSelection = true;
  1163. break;
  1164. case InputEvent::EVENT_MOUSEUP:
  1165. draggingSelection = false;
  1166. break;
  1167. case InputEvent::EVENT_DOUBLECLICK:
  1168. selectWordAtCaret();
  1169. break;
  1170. case InputEvent::EVENT_MOUSEMOVE:
  1171. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  1172. if(draggingSelection) {
  1173. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1174. }
  1175. break;
  1176. case InputEvent::EVENT_MOUSEOVER:
  1177. CoreServices::getInstance()->getCore()->setCursor(CURSOR_TEXT);
  1178. break;
  1179. case InputEvent::EVENT_MOUSEOUT:
  1180. CoreServices::getInstance()->getCore()->setCursor(CURSOR_ARROW);
  1181. break;
  1182. }
  1183. }
  1184. if(event->getDispatcher() == blinkTimer) {
  1185. if(hasSelection || draggingSelection) {
  1186. blinkerRect->visible = false;
  1187. } else {
  1188. if(hasFocus)
  1189. blinkerRect->visible = !blinkerRect->visible;
  1190. else
  1191. blinkerRect->visible = false;
  1192. }
  1193. }
  1194. }