PolyUITextInput.cpp 38 KB

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