PolyUITextInput.cpp 38 KB

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