PolyUITextInput.cpp 43 KB

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