PolyUITextInput.cpp 39 KB

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