PolyUITextInput.cpp 44 KB

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