PolyUITextInput.cpp 45 KB

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