PolyUITextInput.cpp 46 KB

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