PolyUITextInput.cpp 44 KB

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