PolyUITextInput.cpp 41 KB

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