PolyUITextInput.cpp 45 KB

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