PolyUITextInput.cpp 39 KB

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