PolyUITextInput.cpp 46 KB

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