PolyUITextInput.cpp 46 KB

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