PolyUITextInput.cpp 41 KB

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