PolyUITextInput.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  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(fabs(this->getWidth() - textContainer->getPosition2D().x - padding));
  96. textContainer->setHeight(fabs(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. textContainer->setWidth(fabs(this->getWidth() - textContainer->getPosition2D().x - padding));
  385. textContainer->setHeight(fabs(this->getHeight() - textContainer->getPosition2D().y));
  386. textContainer->setPosition(padding + decoratorOffset, padding);
  387. if(multiLine) {
  388. inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
  389. neededBufferLines = (height / ( lineHeight+lineSpacing)) + 1;
  390. checkBufferLines();
  391. renumberLines();
  392. applySyntaxFormatting();
  393. }
  394. if(multiLine && lineNumbersEnabled) {
  395. lineNumberBg->setShapeSize(decoratorOffset, height);
  396. }
  397. if(scrollContainer) {
  398. scrollContainer->Resize(width, height);
  399. }
  400. }
  401. int UITextInput::insertLine(bool after) {
  402. numLines++;
  403. if(after) {
  404. String newText = "";
  405. if(lines.size() > 0) {
  406. String ctext = lines[lineOffset];
  407. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  408. ctext = ctext.substr(0,caretPosition);
  409. lines[lineOffset] = ctext;
  410. newText = text2;
  411. caretPosition=0;
  412. }
  413. vector<String>::iterator it;
  414. lineOffset = lineOffset + 1;
  415. if(lineOffset >= lines.size()) {
  416. it = lines.end();
  417. } else {
  418. it = lines.begin() + lineOffset;
  419. }
  420. lines.insert(it,newText);
  421. renumberLines();
  422. restructLines();
  423. } else {
  424. // do we even need that? I don't think so.
  425. }
  426. changedText();
  427. return 1;
  428. }
  429. void UITextInput::enableLineNumbers(bool val) {
  430. lineNumbersEnabled = val;
  431. lineNumberBg->visible = lineNumbersEnabled;
  432. restructLines();
  433. }
  434. void UITextInput::renumberLines() {
  435. if(!multiLine)
  436. return;
  437. int totalLineNumber = lines.size();
  438. if(currentBufferLines > lines.size()) {
  439. totalLineNumber = currentBufferLines;
  440. }
  441. decoratorOffset = 0;
  442. if(multiLine) {
  443. if(lineNumbersEnabled) {
  444. decoratorOffset = 15;
  445. if(totalLineNumber > 9) {
  446. decoratorOffset = 25;
  447. }
  448. if(totalLineNumber > 99) {
  449. decoratorOffset = 35;
  450. }
  451. if(totalLineNumber > 999) {
  452. decoratorOffset = 45;
  453. }
  454. if(totalLineNumber > 9999) {
  455. decoratorOffset = 55;
  456. }
  457. }
  458. }
  459. lineNumberAnchor->setPositionX(padding+decoratorOffset - 10);
  460. // Update the position and width of the text accordingly.
  461. textContainer->setPosition(decoratorOffset + padding, padding);
  462. textContainer->setWidth(fabs(this->getWidth() - textContainer->getPosition2D().x - padding));
  463. textContainer->setHeight(fabs(this->getHeight() - textContainer->getPosition2D().y - padding));
  464. textContainer->scissorBox.setRect(textContainer->getPosition2D().x, textContainer->getPosition2D().y, textContainer->getWidth(), textContainer->getHeight()+padding);
  465. }
  466. void UITextInput::restructLines() {
  467. for(int i=0; i < bufferLines.size(); i++) {
  468. bufferLines[i]->setPosition(0, (i*(lineHeight+lineSpacing)),0.0f);
  469. }
  470. if(multiLine && lineNumbersEnabled) {
  471. lineNumberBg->setShapeSize(decoratorOffset, height);
  472. }
  473. if(scrollContainer) {
  474. scrollContainer->setContentSize(width, (((lines.size()+1) * ((lineHeight+lineSpacing)))) + padding);
  475. }
  476. if(multiLine) {
  477. inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
  478. }
  479. }
  480. void UITextInput::setCaretPosition(int position) {
  481. if(position >= 0 && position < lines[lineOffset].length()) {
  482. caretPosition = position;
  483. updateCaretPosition();
  484. }
  485. }
  486. void UITextInput::setText(String text, bool sendChangeEvent) {
  487. if(!multiLine) {
  488. lines[lineOffset] = text;
  489. caretPosition = text.length();
  490. clearSelection();
  491. updateCaretPosition();
  492. } else {
  493. selectAll();
  494. insertText(text);
  495. clearSelection();
  496. }
  497. // needFullRedraw = true;
  498. changedText(sendChangeEvent);
  499. }
  500. void UITextInput::onLoseFocus() {
  501. blinkerRect->visible = false;
  502. clearSelection();
  503. }
  504. String UITextInput::getText() {
  505. if(!multiLine) {
  506. return lines[0];
  507. } else {
  508. String totalText = L"";
  509. for(int i=0; i < lines.size(); i++) {
  510. totalText += lines[i];
  511. if(i < lines.size()-1)
  512. totalText += L"\n";
  513. }
  514. return totalText;
  515. }
  516. }
  517. void UITextInput::updateCaretPosition() {
  518. // If this assertion fails, you likely called the function in the wrong
  519. // place in the constructor.
  520. assert(bufferLines.size() && lines.size());
  521. if(lineOffset > lines.size()-1)
  522. lineOffset = lines.size()-1;
  523. caretImagePosition = 0;
  524. if(caretPosition == 0) {
  525. caretImagePosition = 0;
  526. } else if(caretPosition > lines[lineOffset].length()) {
  527. caretPosition = lines[lineOffset].length();
  528. String caretSubString = lines[lineOffset].substr(0,caretPosition);
  529. caretImagePosition = bufferLines[0]->getLabel()->getTextWidthForString(caretSubString);
  530. } else {
  531. String caretSubString = lines[lineOffset].substr(0,caretPosition);
  532. caretImagePosition = bufferLines[0]->getLabel()->getTextWidthForString(caretSubString);
  533. }
  534. blinkerRect->visible = true;
  535. blinkTimer->Reset();
  536. if(doSelectToCaret) {
  537. doSelectToCaret = false;
  538. }
  539. if(!multiLine) {
  540. // Make sure the new caret position is visible.
  541. // Try scrolling left.
  542. while(caretImagePosition <= horizontalPixelScroll && horizontalCharacterScroll > 0) {
  543. horizontalCharacterScroll--;
  544. // Update pixel scroll from new character scroll.
  545. String subString = lines[0].substr(0,horizontalCharacterScroll);
  546. horizontalPixelScroll = bufferLines[0]->getLabel()->getTextWidthForString(subString);
  547. }
  548. // Try scrolling right.
  549. while(caretImagePosition + blinkerRect->getWidth() > horizontalPixelScroll + textContainer->getWidth()) {
  550. horizontalCharacterScroll++;
  551. // Update pixel scroll from new character scroll.
  552. String subString = lines[0].substr(0,horizontalCharacterScroll);
  553. horizontalPixelScroll = bufferLines[0]->getLabel()->getTextWidthForString(subString);
  554. }
  555. bufferLines[0]->setPosition(-horizontalPixelScroll, 0);
  556. }
  557. /*
  558. if(multiLine) {
  559. if(linesContainer->getPosition().y + currentLine->getPosition2D().y < 0.0) {
  560. scrollContainer->scrollVertical(-(lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  561. } else if(linesContainer->getPosition().y + currentLine->getPosition2D().y > scrollContainer->getHeight()) {
  562. scrollContainer->scrollVertical((lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
  563. }
  564. }
  565. */
  566. }
  567. void UITextInput::selectLineFromOffset() {
  568. // TODO: remove or fill out body
  569. }
  570. void UITextInput::dragSelectionTo(Number x, Number y) {
  571. x -= (padding * 2.0) + decoratorOffset;
  572. y -= padding;
  573. int lineOffset = y / (lineHeight+lineSpacing);
  574. if(lineOffset > lines.size()-1)
  575. lineOffset = lines.size()-1;
  576. String selectToLine = lines[lineOffset];
  577. int len = selectToLine.length();
  578. Number slen = 0;
  579. int caretPosition = bufferLines[0]->getLabel()->getTextWidthForString(selectToLine.substr(0,len)) - horizontalPixelScroll;
  580. for(int i=0; i < len; i++) {
  581. slen = bufferLines[0]->getLabel()->getTextWidthForString(selectToLine.substr(0,i)) - horizontalPixelScroll;
  582. if(slen > x) {
  583. caretPosition = i;
  584. break;
  585. }
  586. }
  587. if(x > slen)
  588. caretPosition = len;
  589. // if(multiLine)
  590. // caretPosition++;
  591. if(caretPosition < 0)
  592. caretPosition = 0;
  593. setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
  594. }
  595. int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
  596. for(int i=caretPosition; i > 0; i--) {
  597. String bit = lines[caretLine].substr(i,1);
  598. char chr = ((char*)bit.c_str())[0];
  599. if(!isNumberOrCharacter(chr) && i < caretPosition-1) {
  600. return i+1;
  601. }
  602. }
  603. return 0;
  604. }
  605. int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
  606. int len = lines[caretLine].length();
  607. for(int i=caretPosition; i < len; i++) {
  608. String bit = lines[caretLine].substr(i,1);
  609. char chr = ((char*)bit.c_str())[0];
  610. if(!isNumberOrCharacter(chr) && i > caretPosition) {
  611. return i;
  612. }
  613. }
  614. return lines[caretLine].length();
  615. }
  616. void UITextInput::selectWordAtCaret() {
  617. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  618. clearSelection();
  619. updateCaretPosition();
  620. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  621. updateCaretPosition();
  622. }
  623. void UITextInput::replaceAll(String what, String withWhat) {
  624. for(int i=0; i < lines.size(); i++) {
  625. lines[i] = lines[i].replace(what, withWhat);
  626. }
  627. needFullRedraw = true;
  628. changedText();
  629. }
  630. void UITextInput::findString(String stringToFind, bool replace, String replaceString) {
  631. clearSelection();
  632. findMatches.clear();
  633. for(int i=0; i < lines.size(); i++) {
  634. String lineText = lines[i];
  635. int offset = 0;
  636. int retVal = -1;
  637. do {
  638. retVal = lineText.find(stringToFind, offset);
  639. if(retVal != -1) {
  640. FindMatch match;
  641. match.lineNumber = i;
  642. match.caretStart = retVal;
  643. match.caretEnd = retVal + stringToFind.length();
  644. findMatches.push_back(match);
  645. offset = retVal + stringToFind.length();
  646. }
  647. } while(retVal != -1);
  648. }
  649. if(findMatches.size() > 0) {
  650. if(replace) {
  651. FindMatch match = findMatches[findIndex];
  652. String oldText = lines[match.lineNumber];
  653. String newText = oldText.substr(0,match.caretStart) + replaceString + oldText.substr(match.caretEnd);
  654. lines[match.lineNumber] = newText;
  655. findMatches[findIndex].caretEnd = findMatches[findIndex].caretStart + replaceString.length();
  656. changedText();
  657. }
  658. findIndex = 0;
  659. findCurrent();
  660. }
  661. }
  662. void UITextInput::findNext() {
  663. if(findMatches.size() == 0)
  664. return;
  665. findIndex++;
  666. if(findIndex == findMatches.size()) {
  667. findIndex = 0;
  668. }
  669. findCurrent();
  670. }
  671. void UITextInput::findPrevious() {
  672. if(findMatches.size() == 0)
  673. return;
  674. findIndex--;
  675. if(findIndex < 0) {
  676. findIndex = findMatches.size()-1;
  677. }
  678. findCurrent();
  679. }
  680. void UITextInput::findCurrent() {
  681. if(findMatches.size() == 0)
  682. return;
  683. FindMatch match = findMatches[findIndex];
  684. lineOffset = match.lineNumber;
  685. caretPosition = match.caretStart;
  686. lineOffset = match.lineNumber;
  687. updateCaretPosition();
  688. showLine(findMatches[findIndex].lineNumber, false);
  689. setSelection(match.lineNumber, match.lineNumber, match.caretStart, match.caretEnd);
  690. }
  691. void UITextInput::setCaretToMouse(Number x, Number y) {
  692. clearSelection();
  693. x -= (padding) + decoratorOffset;
  694. y -= padding;
  695. //if(lines.size() > 1) {
  696. lineOffset = y / (lineHeight+lineSpacing);
  697. if(lineOffset > lines.size()-1)
  698. lineOffset = lines.size()-1;
  699. selectLineFromOffset();
  700. //}
  701. int len = lines[lineOffset].length();
  702. Number slen= 0;
  703. int newCaretPosition = -1;
  704. for(int i=1; i < len; i++) {
  705. slen = bufferLines[0]->getLabel()->getTextWidthForString(lines[lineOffset].substr(0,i)) - horizontalPixelScroll;
  706. Number slen_prev = bufferLines[0]->getLabel()->getTextWidthForString(lines[lineOffset].substr(0,i-1)) - horizontalPixelScroll;
  707. if(x >= slen_prev && x <= slen) {
  708. if(x < slen_prev + ((slen - slen_prev) /2.0)) {
  709. newCaretPosition = i-1;
  710. break;
  711. } else {
  712. newCaretPosition = i;
  713. break;
  714. }
  715. }
  716. }
  717. if(newCaretPosition == -1)
  718. newCaretPosition = 0;
  719. if(x > slen)
  720. newCaretPosition = len;
  721. caretPosition = newCaretPosition;
  722. updateCaretPosition();
  723. }
  724. void UITextInput::removeLines(unsigned int startIndex, unsigned int endIndex) {
  725. lines.erase(lines.begin()+startIndex, lines.begin()+endIndex);
  726. renumberLines();
  727. restructLines();
  728. changedText();
  729. }
  730. void UITextInput::selectAll() {
  731. setSelection(0, lines.size()-1, 0, lines[lines.size()-1].length());
  732. }
  733. void UITextInput::insertText(String text) {
  734. vector<String> strings = text.split("\n");
  735. settingText = true;
  736. if(hasSelection)
  737. deleteSelection();
  738. if(strings.size() > 1) {
  739. String ctext = lines[lineOffset];
  740. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  741. ctext = ctext.substr(0,caretPosition);
  742. ctext += strings[0];
  743. lines[lineOffset] = ctext;
  744. caretPosition = ctext.length();
  745. for(int i=1; i < strings.size()-1; i++) {
  746. insertLine(true);
  747. ctext = strings[i];
  748. lines[lineOffset] = ctext;
  749. caretPosition = ctext.length();
  750. }
  751. insertLine(true);
  752. ctext = strings[strings.size()-1] + text2;
  753. caretPosition = ctext.length();
  754. lines[lineOffset] = ctext;
  755. } else {
  756. String ctext = lines[lineOffset];
  757. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  758. ctext = ctext.substr(0,caretPosition);
  759. ctext += text + text2;
  760. caretPosition += text.length();
  761. lines[lineOffset] = ctext;
  762. }
  763. settingText = false;
  764. restructLines();
  765. renumberLines();
  766. changedText();
  767. updateCaretPosition();
  768. }
  769. String UITextInput::getLineText(unsigned int index) {
  770. if(index < lines.size()) {
  771. return lines[index];
  772. } else {
  773. return "";
  774. }
  775. }
  776. String UITextInput::getSelectionText() {
  777. if(!hasSelection)
  778. return L"";
  779. String totalText = L"";
  780. // Set up iteration cursors
  781. int currentLine = selectionTop;
  782. int currentLeft = selectionL;
  783. // Iterate over the inner lines(we'll be appending \n to these)
  784. while(currentLine < selectionBottom) {
  785. totalText += lines[currentLine].substr(currentLeft, lines[currentLine].length()-currentLeft) + '\n';
  786. currentLine++;
  787. currentLeft = 0;
  788. }
  789. // Add the selection in the last line(no \n needed)
  790. totalText += lines[currentLine].substr(currentLeft, selectionR-currentLeft);
  791. return totalText;
  792. }
  793. void UITextInput::setSelectionColor(Color color) {
  794. selectorRectTop->color = color;
  795. selectorRectMiddle->color = color;
  796. selectorRectBottom->color = color;
  797. }
  798. void UITextInput::setCursorColor(Color color) {
  799. blinkerRect->color = color;
  800. }
  801. void UITextInput::setBackgroundColor(Color color) {
  802. inputRect->color = color;
  803. }
  804. void UITextInput::setLineNumberColor(Color color) {
  805. lineNumberColor = color;
  806. for(int i=0; i < numberLines.size(); i++) {
  807. numberLines[i]->color = lineNumberColor;
  808. }
  809. }
  810. void UITextInput::setTextColor(Color color) {
  811. textColor = color;
  812. for(int i=0; i < bufferLines.size(); i++) {
  813. bufferLines[i]->color = textColor;
  814. }
  815. }
  816. UIScrollContainer *UITextInput::getScrollContainer() {
  817. return scrollContainer;
  818. }
  819. void UITextInput::saveUndoState() {
  820. UITextInputUndoState newState;
  821. newState.content = getText();
  822. newState.caretPosition = caretPosition;
  823. newState.lineOffset = lineOffset;
  824. newState.hasSelection = hasSelection;
  825. if(hasSelection) {
  826. newState.selectionLine = selectionLine;
  827. newState.selectionCaretPosition = selectionCaretPosition;
  828. }
  829. undoStates[undoStateIndex] = newState;
  830. // if we hit undo state capacity, shift the whole stack
  831. if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
  832. for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
  833. undoStates[i] = undoStates[i+1];
  834. }
  835. } else {
  836. undoStateIndex++;
  837. }
  838. maxRedoIndex = undoStateIndex;
  839. // By default, reset the isTypingWord status.
  840. // If we are typing a word after all, the caller
  841. // will immediately reset it to 1.
  842. isTypingWord = 0;
  843. }
  844. void UITextInput::setUndoState(UITextInputUndoState state) {
  845. clearSelection();
  846. setText(state.content);
  847. lineOffset = state.lineOffset;
  848. caretPosition = state.caretPosition;
  849. lineOffset = state.lineOffset;
  850. updateCaretPosition();
  851. if(state.hasSelection) {
  852. setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
  853. }
  854. showLine(state.lineOffset, false);
  855. }
  856. void UITextInput::Undo() {
  857. if(undoStateIndex > 0) {
  858. undoStateIndex--;
  859. setUndoState(undoStates[undoStateIndex]);
  860. }
  861. }
  862. void UITextInput::Redo() {
  863. if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
  864. undoStateIndex++;
  865. setUndoState(undoStates[undoStateIndex]);
  866. }
  867. }
  868. void UITextInput::Cut() {
  869. saveUndoState();
  870. Copy();
  871. if(hasSelection) {
  872. deleteSelection();
  873. } else if (getLineText(lineOffset) != "") {
  874. if (!multiLine) { setText(""); }
  875. else {
  876. removeLines(lineOffset, lineOffset+1);
  877. caretPosition = 0;
  878. updateCaretPosition();
  879. }
  880. }
  881. }
  882. void UITextInput::Copy() {
  883. if(hasSelection) {
  884. CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
  885. } else {
  886. if (getLineText(lineOffset) != "") {
  887. CoreServices::getInstance()->getCore()->copyStringToClipboard(getLineText(lineOffset));
  888. }
  889. }
  890. }
  891. void UITextInput::Paste() {
  892. saveUndoState();
  893. insertText(CoreServices::getInstance()->getCore()->getClipboardString());
  894. }
  895. void UITextInput::showLine(unsigned int lineNumber, bool top) {
  896. // If there's no scroll container, no need to adjust the line.
  897. if(!multiLine) {
  898. return;
  899. }
  900. if(top) {
  901. scrollContainer->setScrollValue(0.0, ((((lineNumber) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
  902. } else {
  903. scrollContainer->setScrollValue(0.0, (((((lineNumber) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()/2.0))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
  904. }
  905. }
  906. bool UITextInput::isNumberOrCharacter(wchar_t charCode) {
  907. if(charCode > 47 && charCode < 58)
  908. return true;
  909. if(charCode > 64 && charCode < 91)
  910. return true;
  911. if(charCode > 96 && charCode < 123)
  912. return true;
  913. return false;
  914. }
  915. void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
  916. if(!hasFocus)
  917. return;
  918. // Logger::log("UCHAR: %d\n", charCode);
  919. CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
  920. if(key == KEY_LEFT) {
  921. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  922. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  923. if(hasSelection) {
  924. setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
  925. } else {
  926. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
  927. }
  928. } else {
  929. caretPosition = 0;
  930. clearSelection();
  931. updateCaretPosition();
  932. }
  933. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  934. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  935. if(hasSelection) {
  936. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
  937. } else {
  938. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
  939. }
  940. } else {
  941. caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
  942. clearSelection();
  943. updateCaretPosition();
  944. }
  945. } else {
  946. if(caretPosition > 0 || lineOffset > 0) {
  947. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  948. // Holding down shift allows you to select with the arrow keys.
  949. if(hasSelection) {
  950. if(selectionCaretPosition > 0)
  951. setSelection(lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
  952. } else {
  953. setSelection(lineOffset, lineOffset, caretPosition, max(caretPosition-1, 0));
  954. }
  955. } else {
  956. int newLineStart = lineOffset;
  957. int newCaretPosition = caretPosition;
  958. if(newCaretPosition > 0) {
  959. newCaretPosition--;
  960. } else if(newLineStart > 0) {
  961. newLineStart--;
  962. newCaretPosition = lines[newLineStart].length();
  963. }
  964. clearSelection();
  965. caretPosition = newCaretPosition;
  966. lineOffset = newLineStart;
  967. updateCaretPosition();
  968. }
  969. }
  970. }
  971. return;
  972. }
  973. if(key == KEY_RIGHT) {
  974. if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
  975. if(caretPosition < lines[lineOffset].length()) {
  976. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  977. if(hasSelection) {
  978. setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine].length());
  979. } else {
  980. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, lines[lineOffset].length());
  981. }
  982. } else {
  983. caretPosition = lines[lineOffset].length();
  984. clearSelection();
  985. updateCaretPosition();
  986. }
  987. }
  988. } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
  989. if(caretPosition < lines[lineOffset].length()) {
  990. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  991. if(hasSelection) {
  992. setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
  993. } else {
  994. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
  995. }
  996. } else {
  997. caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
  998. clearSelection();
  999. updateCaretPosition();
  1000. }
  1001. }
  1002. } else {
  1003. if(caretPosition < lines[lineOffset].length() || lineOffset + 1 < lines.size()) {
  1004. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  1005. // Holding down shift allows you to select with the arrow keys.
  1006. if(hasSelection) {
  1007. setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
  1008. } else {
  1009. setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
  1010. }
  1011. } else {
  1012. clearSelection();
  1013. int newLineEnd = lineOffset;
  1014. int newCaretPosition = caretPosition;
  1015. if(newCaretPosition < lines[lineOffset].length()) {
  1016. newCaretPosition++;
  1017. } else if(newLineEnd + 1 < lines.size()) {
  1018. newLineEnd++;
  1019. newCaretPosition = 0;
  1020. }
  1021. caretPosition = newCaretPosition;
  1022. lineOffset = newLineEnd;
  1023. }
  1024. updateCaretPosition();
  1025. }
  1026. }
  1027. return;
  1028. }
  1029. if(key == KEY_PAGEUP) {
  1030. if(multiLine) {
  1031. scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  1032. }
  1033. return;
  1034. }
  1035. if(key == KEY_PAGEDOWN) {
  1036. if(multiLine) {
  1037. scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
  1038. }
  1039. return;
  1040. }
  1041. if(key == KEY_UP) {
  1042. if(multiLine) {
  1043. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  1044. if(hasSelection) {
  1045. if(selectionLine > 0)
  1046. setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
  1047. } else {
  1048. if(this->lineOffset > 0)
  1049. setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
  1050. }
  1051. } else {
  1052. clearSelection();
  1053. if(lineOffset > 0) {
  1054. lineOffset--;
  1055. selectLineFromOffset();
  1056. updateCaretPosition();
  1057. }
  1058. }
  1059. if(linesContainer->getPosition().y + (lineOffset*(lineHeight+lineSpacing)+padding) < 0.0) {
  1060. scrollContainer->setScrollValue(0.0, ((((lineOffset) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
  1061. }
  1062. }
  1063. blinkerRect->visible = true;
  1064. return;
  1065. }
  1066. if(key == KEY_DOWN) {
  1067. if(multiLine) {
  1068. if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
  1069. if(hasSelection) {
  1070. if(selectionLine < lines.size()-1)
  1071. setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
  1072. } else {
  1073. if(this->lineOffset < lines.size()-1)
  1074. setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
  1075. }
  1076. } else {
  1077. clearSelection();
  1078. if(lineOffset < lines.size()-1) {
  1079. lineOffset++;
  1080. selectLineFromOffset();
  1081. updateCaretPosition();
  1082. }
  1083. }
  1084. if(linesContainer->getPosition().y + (lineOffset*(lineHeight+lineSpacing)+padding) > scrollContainer->getHeight()-lineHeight-lineSpacing) {
  1085. scrollContainer->setScrollValue(0.0, (((((lineOffset) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()-lineHeight-lineSpacing))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
  1086. }
  1087. }
  1088. blinkerRect->visible = true;
  1089. return;
  1090. }
  1091. if(key == KEY_ESCAPE) {
  1092. if(!multiLine) {
  1093. dispatchEvent(new Event(), Event::CANCEL_EVENT);
  1094. }
  1095. }
  1096. if(key == KEY_RETURN) {
  1097. if(multiLine) {
  1098. saveUndoState();
  1099. if(hasSelection) {
  1100. deleteSelection();
  1101. }
  1102. insertLine(true);
  1103. updateCaretPosition();
  1104. if(linesContainer->getPosition().y + (lineOffset*(lineHeight+lineSpacing)+padding) > scrollContainer->getHeight()-lineHeight-lineSpacing) {
  1105. scrollContainer->setScrollValue(0.0, (((((lineOffset) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()-lineHeight-lineSpacing))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
  1106. }
  1107. } else {
  1108. dispatchEvent(new Event(), Event::COMPLETE_EVENT);
  1109. }
  1110. return;
  1111. }
  1112. // indent/shift text
  1113. if (multiLine && (key == KEY_LEFTBRACKET || key == KEY_RIGHTBRACKET) &&
  1114. (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER) ||
  1115. input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))) {
  1116. shiftText( (key == KEY_RIGHTBRACKET) ? false : true );
  1117. return;
  1118. }
  1119. // at this point, return if certain modifier keys are held down so as not to potentially add any unwanted text
  1120. if (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER) || input->getKeyState(KEY_LCTRL) ||
  1121. input->getKeyState(KEY_RCTRL) || input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT))
  1122. return;
  1123. String ctext = lines[lineOffset];
  1124. bool _changedText = false;
  1125. if((charCode > 31 && charCode < 127) || charCode > 127) {
  1126. if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
  1127. if(!isNumberOrCharacter(charCode)) {
  1128. saveUndoState();
  1129. } else if (!isTypingWord) {
  1130. saveUndoState();
  1131. isTypingWord = 1;
  1132. }
  1133. if(hasSelection)
  1134. deleteSelection();
  1135. ctext = lines[lineOffset];
  1136. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1137. ctext = ctext.substr(0,caretPosition);
  1138. ctext += charCode + text2;
  1139. caretPosition++;
  1140. _changedText = true;
  1141. }
  1142. }
  1143. if(key == KEY_TAB && multiLine) {
  1144. saveUndoState();
  1145. if(hasSelection)
  1146. deleteSelection();
  1147. ctext = lines[lineOffset];
  1148. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1149. ctext = ctext.substr(0,caretPosition);
  1150. ctext += (wchar_t)'\t' + text2;
  1151. caretPosition++;
  1152. _changedText = true;
  1153. }
  1154. if(key == KEY_BACKSPACE) {
  1155. if(hasSelection) {
  1156. saveUndoState();
  1157. deleteSelection();
  1158. return;
  1159. } else {
  1160. ctext = lines[lineOffset];
  1161. if(caretPosition > 0) {
  1162. saveUndoState();
  1163. if(ctext.length() > 0) {
  1164. String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
  1165. ctext = ctext.substr(0,caretPosition-1);
  1166. ctext += text2;
  1167. caretPosition--;
  1168. _changedText = true;
  1169. }
  1170. } else {
  1171. if(lineOffset > 0) {
  1172. saveUndoState();
  1173. lineOffset--;
  1174. selectLineFromOffset();
  1175. caretPosition = lines[lineOffset].length();
  1176. updateCaretPosition();
  1177. lines[lineOffset] = lines[lineOffset] + ctext;
  1178. removeLines(lineOffset+1, lineOffset+2);
  1179. return;
  1180. }
  1181. }
  1182. }
  1183. if (multiLine) {
  1184. if(linesContainer->getPosition().y + (lineOffset*(lineHeight+lineSpacing)+padding) < 0.0) {
  1185. scrollContainer->setScrollValue(0.0, ((((lineOffset) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
  1186. }
  1187. }
  1188. }
  1189. lines[lineOffset] = ctext;
  1190. if(_changedText) {
  1191. changedText();
  1192. }
  1193. updateCaretPosition();
  1194. }
  1195. void UITextInput::Update() {
  1196. if(hasSelection) {
  1197. blinkerRect->visible = false;
  1198. }
  1199. blinkerRect->setPosition(caretImagePosition + 1 -horizontalPixelScroll, (lineOffset * ( lineHeight+lineSpacing)));
  1200. if(hasFocus) {
  1201. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
  1202. } else {
  1203. blinkerRect->visible = false;
  1204. // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  1205. }
  1206. for(int i=0; i < linesToDelete.size(); i++) {
  1207. delete linesToDelete[i];
  1208. }
  1209. linesToDelete.clear();
  1210. textContainer->scissorBox.setRect(textContainer->getScreenPosition().x, textContainer->getScreenPosition().y, textContainer->getWidth(), textContainer->getHeight() + padding);
  1211. }
  1212. UITextInput::~UITextInput() {
  1213. core->removeAllHandlersForListener(this);
  1214. delete blinkTimer;
  1215. linesContainer->ownsChildren = true;
  1216. if(!ownsChildren) {
  1217. delete linesContainer;
  1218. delete inputRect;
  1219. delete lineNumberBg;
  1220. }
  1221. }
  1222. void UITextInput::readjustBuffer() {
  1223. int bufferOffset = -linesContainer->position.y/ ( lineHeight+lineSpacing);
  1224. Number bufferLineOffset = bufferOffset * ( lineHeight+lineSpacing);
  1225. for(int i=0; i < bufferLines.size(); i++) {
  1226. bufferLines[i]->getLabel()->clearColors();
  1227. if(bufferOffset + i < lines.size()) {
  1228. if(bufferOffset+i < lineColors.size()) {
  1229. for(int j=0; j < lineColors[bufferOffset+i].colors.size(); j++) {
  1230. bufferLines[i]->getLabel()->setColorForRange(lineColors[bufferOffset+i].colors[j].color, lineColors[bufferOffset+i].colors[j].rangeStart, lineColors[bufferOffset+i].colors[j].rangeEnd);
  1231. bufferLines[i]->setColor(1.0, 1.0, 1.0, 1.0);
  1232. }
  1233. }
  1234. bufferLines[i]->setText(lines[bufferOffset+i]);
  1235. } else {
  1236. bufferLines[i]->setText("");
  1237. }
  1238. bufferLines[i]->setPosition(-horizontalPixelScroll, bufferLineOffset + (i*(lineHeight+lineSpacing)),0.0f);
  1239. }
  1240. for(int i=0; i < numberLines.size(); i++) {
  1241. if(lineNumbersEnabled) {
  1242. numberLines[i]->setText(String::IntToString(bufferOffset+i+1));
  1243. int textWidth = ceil(numberLines[i]->getLabel()->getTextWidth());
  1244. numberLines[i]->setPosition(-textWidth,padding + bufferLineOffset + (i*(lineHeight+lineSpacing)),0.0f);
  1245. numberLines[i]->visible = true;
  1246. }
  1247. }
  1248. }
  1249. void UITextInput::handleEvent(Event *event) {
  1250. if(event->getDispatcher() == core && hasFocus) {
  1251. switch(event->getEventCode()) {
  1252. case Core::EVENT_UNDO:
  1253. Undo();
  1254. break;
  1255. case Core::EVENT_REDO:
  1256. Redo();
  1257. break;
  1258. case Core::EVENT_COPY:
  1259. Copy();
  1260. break;
  1261. case Core::EVENT_CUT:
  1262. Cut();
  1263. break;
  1264. case Core::EVENT_PASTE:
  1265. Paste();
  1266. break;
  1267. case Core::EVENT_SELECT_ALL:
  1268. selectAll();
  1269. break;
  1270. }
  1271. }
  1272. if(event->getDispatcher() == scrollContainer) {
  1273. if(event->getEventCode() == Event::CHANGE_EVENT) {
  1274. applySyntaxFormatting();
  1275. }
  1276. }
  1277. if(event->getDispatcher() == inputRect) {
  1278. switch(event->getEventCode()) {
  1279. case InputEvent::EVENT_MOUSEDOWN:
  1280. if(parentEntity) {
  1281. ((ScreenEntity*)parentEntity)->focusChild(this);
  1282. } else {
  1283. hasFocus = true;
  1284. }
  1285. setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1286. draggingSelection = true;
  1287. break;
  1288. case InputEvent::EVENT_MOUSEUP:
  1289. draggingSelection = false;
  1290. break;
  1291. case InputEvent::EVENT_DOUBLECLICK:
  1292. selectWordAtCaret();
  1293. break;
  1294. case InputEvent::EVENT_MOUSEMOVE:
  1295. CoreServices::getInstance()->getCore()->setCursor(Core::CURSOR_TEXT);
  1296. if(draggingSelection) {
  1297. dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
  1298. }
  1299. break;
  1300. case InputEvent::EVENT_MOUSEOVER:
  1301. CoreServices::getInstance()->getCore()->setCursor(Core::CURSOR_TEXT);
  1302. break;
  1303. case InputEvent::EVENT_MOUSEOUT:
  1304. CoreServices::getInstance()->getCore()->setCursor(Core::CURSOR_ARROW);
  1305. break;
  1306. }
  1307. }
  1308. if(event->getDispatcher() == blinkTimer) {
  1309. if(hasSelection || draggingSelection) {
  1310. blinkerRect->visible = false;
  1311. } else {
  1312. if(hasFocus)
  1313. blinkerRect->visible = !blinkerRect->visible;
  1314. else
  1315. blinkerRect->visible = false;
  1316. }
  1317. }
  1318. }
  1319. void UITextInput::shiftText(bool left) {
  1320. if (multiLine && (hasSelection || lines[lineOffset] != "")) {
  1321. saveUndoState();
  1322. String t = (wchar_t)'\t';
  1323. if (hasSelection) {
  1324. for (int i = selectionTop; i <= selectionBottom; i++) {
  1325. if (i == selectionBottom && selectionCaretPosition <= 0)
  1326. // at least one character of bottom line needs to be selected before indenting, so...
  1327. break;
  1328. if (indentType == INDENT_TAB) {
  1329. if (left) {
  1330. if (lines[i].substr(0,1) == t) {
  1331. lines[i] = lines[i].substr(1, lines[i].length()-1);
  1332. caretPosition--;
  1333. }
  1334. } else {
  1335. lines[i] = t + lines[i];
  1336. caretPosition++;
  1337. }
  1338. } else if (indentType == INDENT_SPACE) {
  1339. if (left) {
  1340. // TODO
  1341. } else {
  1342. // TODO
  1343. }
  1344. }
  1345. }
  1346. }
  1347. else {
  1348. if (indentType == INDENT_TAB) {
  1349. if (left) {
  1350. if (lines[lineOffset].substr(0,1) == t) {
  1351. lines[lineOffset] = lines[lineOffset].substr(1, lines[lineOffset].length()-1);
  1352. caretPosition--;
  1353. }
  1354. } else {
  1355. lines[lineOffset] = t + lines[lineOffset];
  1356. caretPosition++;
  1357. }
  1358. } else if (indentType == INDENT_SPACE) {
  1359. if (left) {
  1360. // TODO
  1361. } else {
  1362. // TODO
  1363. }
  1364. }
  1365. }
  1366. changedText();
  1367. updateCaretPosition();
  1368. }
  1369. }
  1370. void UITextInput::convertIndentToSpaces() {
  1371. if (indentType == INDENT_TAB) {
  1372. indentType = INDENT_SPACE;
  1373. //TODO
  1374. }
  1375. }
  1376. void UITextInput::convertIndentToTabs() {
  1377. if (indentType == INDENT_SPACE) {
  1378. indentType = INDENT_TAB;
  1379. //TODO
  1380. }
  1381. }