PolyUITextInput.cpp 45 KB

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