PolyUITextInput.cpp 46 KB

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