PolyUITextInput.cpp 43 KB

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