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