PolyUITextInput.cpp 38 KB

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