| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659 |
- /*
- Copyright (C) 2012 by Ivan Safrin
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- #include "PolyUITextInput.h"
- #include "PolyConfig.h"
- #include "PolyInputEvent.h"
- #include "PolyLabel.h"
- #include "PolyCoreServices.h"
- #include "PolyEventHandler.h"
- using namespace Polycode;
- UITextInput::UITextInput(bool multiLine, Number width, Number height) : UIElement(width, height) {
- this->multiLine = multiLine;
- processInputEvents = true;
- isNumberOnly = false;
-
- decoratorOffset = 0;
- lineOffset = -1;
-
- useStrongHinting = false;
-
- draggingSelection = false;
- hasSelection = false;
- doSelectToCaret = false;
-
- lineNumbersEnabled = false;
-
- caretPosition = 0;
- caretImagePosition = 0;
- horizontalPixelScroll = 0;
- horizontalCharacterScroll = 0;
-
- settingText = false;
-
- needFullRedraw = false;
- isTypingWord = false;
-
- numLines = 0;
-
- this->positionMode = ScreenEntity::POSITION_TOPLEFT;
- Config *conf = CoreServices::getInstance()->getConfig();
-
- if(multiLine)
- fontName = conf->getStringValue("Polycode", "uiTextInputFontNameMultiLine");
- else
- fontName = conf->getStringValue("Polycode", "uiTextInputFontName");
-
- if(multiLine)
- fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSizeMultiline");
- else
- fontSize = conf->getNumericValue("Polycode", "uiTextInputFontSize");
-
- Number rectHeight = height;
- if(!multiLine) {
- rectHeight = fontSize+12;
- }
- linesContainer = new ScreenEntity();
- linesContainer->processInputEvents = true;
- linesContainer->ownsChildren = true;
- lineSpacing = conf->getNumericValue("Polycode", "textEditLineSpacing");
-
- st = conf->getNumericValue("Polycode", "textBgSkinT");
- sr = conf->getNumericValue("Polycode", "textBgSkinR");
- sb = conf->getNumericValue("Polycode", "textBgSkinB");
- sl = conf->getNumericValue("Polycode", "textBgSkinL");
-
- padding = conf->getNumericValue("Polycode", "textBgSkinPadding");
-
- textContainer = new UIElement();
- textContainer->ownsChildren = true;
- textContainer->enableScissor = true;
- linesContainer->addChild(textContainer);
- if(multiLine) {
- inputRect = new UIBox(conf->getStringValue("Polycode", "textBgSkinMultiline"),
- st,sr,sb,sl,
- width+(padding*2), height+(padding*2));
- } else {
- inputRect = new UIBox(conf->getStringValue("Polycode", "textBgSkin"),
- st,sr,sb,sl,
- width+(padding*2), height+(padding*2));
- }
-
- addChild(inputRect);
-
- if(multiLine) {
- lineNumberBg = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
- lineNumberBg->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- lineNumberBg->setColor(0.0, 0.0, 0.0, 0.3);
- addChild(lineNumberBg);
- lineNumberBg->visible = false;
-
- lineNumberAnchor = new ScreenEntity();
- linesContainer->addChild(lineNumberAnchor);
- } else {
- lineNumberBg = NULL;
- lineNumberAnchor = NULL;
- decoratorOffset = sl/2.0;
- }
- textContainer->setWidth(this->getWidth() - textContainer->getPosition2D().x - padding);
- textContainer->setHeight(this->getHeight() - textContainer->getPosition2D().y);
- textContainer->setPosition(padding + decoratorOffset, padding);
-
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
- inputRect->addEventListener(this, InputEvent::EVENT_DOUBLECLICK);
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
- inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
- inputRect->processInputEvents = true;
- inputRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
-
- selectorRectTop = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
- selectorRectTop->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- selectorRectTop->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
- selectorRectTop->visible = false;
- textContainer->addChild(selectorRectTop);
- selectorRectMiddle = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
- selectorRectMiddle->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- selectorRectMiddle->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
- selectorRectMiddle->visible = false;
- textContainer->addChild(selectorRectMiddle);
- selectorRectBottom = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
- selectorRectBottom->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- selectorRectBottom->setColor(181.0f/255.0f, 213.0f/255.0f, 255.0f/255.0f, 1);
- selectorRectBottom->visible = false;
- textContainer->addChild(selectorRectBottom);
-
-
- blinkerRect = new ScreenShape(ScreenShape::SHAPE_RECT, 1, fontSize+2,0,0);
- blinkerRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- blinkerRect->setColor(0,0,0,1);
- textContainer->addChild(blinkerRect);
- blinkerRect->visible = false;
- blinkerRect->setPosition(-horizontalPixelScroll,3);
-
- blinkTimer = new Timer(true, 500);
- blinkTimer->addEventListener(this, Timer::EVENT_TRIGGER);
- focusable = true;
- this->width = width;
- this->height = rectHeight;
- setHitbox(width, rectHeight);
-
- scrollContainer = NULL;
- if(multiLine) {
- scrollContainer = new UIScrollContainer(linesContainer, false, true, 200, 200);
- scrollContainer->addEventListener(this, Event::CHANGE_EVENT);
- addChild(scrollContainer);
- } else {
- addChild(linesContainer);
- }
-
- undoStateIndex = 0;
- maxRedoIndex = 0;
-
- syntaxHighliter = NULL;
-
- textColor = Color(0.0,0.0,0.0,1.0);
- currentBufferLines = 0;
- neededBufferLines = 1;
- checkBufferLines();
- insertLine(true);
- updateCaretPosition();
-
- core = CoreServices::getInstance()->getCore();
- core->addEventListener(this, Core::EVENT_COPY);
- core->addEventListener(this, Core::EVENT_PASTE);
- core->addEventListener(this, Core::EVENT_CUT);
- core->addEventListener(this, Core::EVENT_UNDO);
- core->addEventListener(this, Core::EVENT_REDO);
- core->addEventListener(this, Core::EVENT_SELECT_ALL);
- indentSpacing = 4;
- indentType = INDENT_TAB;
- }
- void UITextInput::checkBufferLines() {
- if(neededBufferLines < currentBufferLines)
- return;
-
- int aaMode = Label::ANTIALIAS_FULL;
- if(useStrongHinting) {
- aaMode = Label::ANTIALIAS_STRONG;
- }
- for(int i=0; i < neededBufferLines - currentBufferLines; i++) {
- if(multiLine) {
- ScreenLabel *newNumberLine = new ScreenLabel(L"", fontSize, fontName, aaMode);
- newNumberLine->color = lineNumberColor;
- lineNumberAnchor->addChild(newNumberLine);
- numberLines.push_back(newNumberLine);
-
- if(!lineNumbersEnabled) {
- newNumberLine->visible = false;
- }
- }
-
- ScreenLabel *newLine = new ScreenLabel(L"", fontSize, fontName, aaMode);
- newLine->color = textColor;
- lineHeight = newLine->getHeight();
- textContainer->addChild(newLine);
- bufferLines.push_back(newLine);
- }
-
- currentBufferLines = neededBufferLines;
- }
- void UITextInput::setNumberOnly(bool val) {
- isNumberOnly = val;
- }
- void UITextInput::clearSelection() {
- hasSelection = false;
- selectorRectTop->visible = false;
- selectorRectMiddle->visible = false;
- selectorRectBottom->visible = false;
- }
- void UITextInput::setSelection(int lineStart, int lineEnd, int colStart, int colEnd) {
- if(lineStart == lineEnd && colStart == colEnd) {
- clearSelection();
- return;
- }
- if(lineStart == lineOffset) {
- selectionLine = lineEnd;
- } else {
- selectionLine = lineStart;
- }
- if(colStart == caretPosition) {
- selectionCaretPosition = colEnd;
- } else {
- selectionCaretPosition = colStart;
- }
-
- // printf("SET lineStart:%d lineEnd:%d colStart:%d colEnd:%d\n", lineStart, lineEnd, colStart, colEnd);
-
- if(lineStart > lineEnd) {
- int tmp = lineStart;
- lineStart = lineEnd;
- lineEnd = tmp;
-
- tmp = colStart;
- colStart = colEnd;
- colEnd = tmp;
- }
-
- if(colStart > colEnd && lineStart == lineEnd) {
- int tmp = colStart;
- colStart = colEnd;
- colEnd = tmp;
- }
-
- clearSelection();
-
- if(lineStart > lines.size()-1)
- return;
- String topLine = lines[lineStart];
-
- if(colStart+1 > topLine.length()) {
- colStart = topLine.length();
- }
-
- Number fColEnd = colEnd;
-
- if(colEnd > topLine.length() || lineStart != lineEnd)
- fColEnd = topLine.length();
- Number topSize, topHeight, topX;
-
- selectorRectTop->visible = true;
- topSize = bufferLines[0]->getLabel()->getTextWidthForString(topLine.substr(colStart,fColEnd-colStart)) ;
- topHeight = lineHeight+lineSpacing;
- if(colStart >= 0) {
- topX = bufferLines[0]->getLabel()->getTextWidthForString(topLine.substr(0,colStart)) + 2;
- } else {
- topX = 0;
- }
- selectorRectTop->setScale(topSize, topHeight);
- selectorRectTop->setPosition(topX + (topSize/2.0) - horizontalPixelScroll, lineStart * (lineHeight+lineSpacing) + (topHeight/2.0));
-
- if(lineEnd > lineStart && lineEnd < lines.size()) {
- String bottomLine = lines[lineEnd];
- selectorRectBottom->visible = true;
- Number bottomSize = bufferLines[0]->getLabel()->getTextWidthForString(bottomLine.substr(0,colEnd)) ;
- if(bottomSize < 0)
- bottomSize = this->width-padding;
- Number bottomHeight = lineHeight+lineSpacing;
- selectorRectBottom->setScale(bottomSize, bottomHeight);
- selectorRectBottom->setPosition(bottomSize/2.0 - horizontalPixelScroll, lineEnd * (lineHeight+lineSpacing) + (bottomHeight/2.0));
-
- if(lineEnd != lineStart+1) {
- // need filler
- selectorRectMiddle->visible = true;
- Number midSize = this->width-padding;
- Number midHeight = 0;
- for(int i=lineStart+1; i < lineEnd;i++) {
- midHeight += lineHeight+lineSpacing;
- }
- selectorRectMiddle->setScale(midSize, midHeight);
- selectorRectMiddle->setPosition(midSize/2.0 - horizontalPixelScroll, ((lineStart+1) * (lineHeight+lineSpacing)) + (midHeight/2.0));
-
- }
-
- }
- hasSelection = true;
-
- selectionTop = lineStart;
- selectionBottom = lineEnd;
- selectionL = colStart;
- selectionR = colEnd;
- }
- void UITextInput::deleteSelection() {
- if(selectionTop == selectionBottom) {
- String ctext = lines[selectionTop];
- String newText = ctext.substr(0, selectionL);
- int rside = selectionR;
- if(rside > ctext.length()-1)
- rside = ctext.length() - 1;
- newText += ctext.substr(rside,ctext.length() - selectionR);
- lines[selectionTop] = newText;
- } else {
-
- String ctext = lines[selectionTop];
- String newText = ctext.substr(0, selectionL);
- lines[selectionTop] = newText;
- ctext = lines[selectionBottom];
-
- int rside = selectionR;
- if(rside > ctext.length()-1)
- rside = ctext.length() - 1;
- newText = ctext.substr(rside,ctext.length() - selectionR);
-
- lineOffset = selectionTop;
- selectLineFromOffset();
- caretPosition = lines[lineOffset].length();
- updateCaretPosition();
- lines[lineOffset] = lines[lineOffset] + newText;
-
- removeLines(selectionTop+1, selectionBottom+1);
-
-
- }
- clearSelection();
- caretPosition = selectionL;
- updateCaretPosition();
- changedText();
- }
- void UITextInput::applySyntaxFormatting() {
- if(syntaxHighliter && multiLine) {
-
- int startLine = (-linesContainer->getPosition().y) / (lineHeight+lineSpacing);
- unsigned int endLine = startLine + ((int)((height / (lineHeight+lineSpacing)))) + 1;
-
- if(startLine < 0)
- startLine = 0;
-
- if(endLine > lines.size())
- endLine = lines.size();
-
- if(needFullRedraw) {
- startLine = 0;
- endLine = lines.size();
- }
-
-
- String totalText = L"";
- for(int i=startLine; i < endLine; i++) {
- totalText += lines[i];
- if(i < lines.size()-1)
- totalText += L"\n";
- }
-
- std::vector<SyntaxHighlightToken> tokens = syntaxHighliter->parseText(totalText);
-
- // DO SYNTAX HIGHLIGHTING
- if(needFullRedraw) {
- lineColors.clear();
- for(int i=0; i < lines.size(); i++) {
- lineColors.push_back(LineColorInfo());
- }
- needFullRedraw = false;
- } else {
- std::vector<LineColorInfo> newInfo;
-
- for(int i=0; i < lines.size(); i++) {
- if((i >= startLine && i < endLine) || i >= lineColors.size()) {
- newInfo.push_back(LineColorInfo());
- } else {
- newInfo.push_back(lineColors[i]);
- }
- }
-
- lineColors = newInfo;
- }
-
- int lineIndex = startLine;
- int rangeStart = 0;
- int rangeEnd = 0;
-
- for(int i=0; i < tokens.size(); i++) {
- if(tokens[i].text == "\n") {
- lineIndex++;
- if(lineIndex >= endLine) {
- lineIndex = endLine-1;
- }
- rangeStart = 0;
- rangeEnd = 0;
- } else {
- if(lineIndex < lines.size()) {
- int textLength = tokens[i].text.length();
- if(tokens[i].text.length() > 1) {
- rangeEnd = rangeStart + textLength-1;
- lineColors[lineIndex].colors.push_back(LineColorData(tokens[i].color, rangeStart, rangeEnd));
- rangeStart = rangeStart + textLength;
- } else {
- rangeEnd = rangeStart;
- lineColors[lineIndex].colors.push_back(LineColorData(tokens[i].color, rangeStart, rangeEnd));
- rangeStart++;
- }
- }
- }
- }
-
- }
- readjustBuffer();
- }
- void UITextInput::changedText(bool sendChangeEvent) {
- if(settingText)
- return;
- applySyntaxFormatting();
- if(sendChangeEvent) {
- dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
- }
- }
- void UITextInput::setSyntaxHighlighter(UITextInputSyntaxHighlighter *syntaxHighliter) {
- this->syntaxHighliter = syntaxHighliter;
- }
- void UITextInput::Resize(Number width, Number height) {
- inputRect->resizeBox(width, height);
- this->width = width;
- this->height = height;
- matrixDirty = true;
- setHitbox(width,height);
-
- textContainer->setWidth(this->getWidth() - textContainer->getPosition2D().x - padding);
- textContainer->setHeight(this->getHeight() - textContainer->getPosition2D().y);
- textContainer->setPosition(padding + decoratorOffset, padding);
-
- if(multiLine) {
- inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
-
- neededBufferLines = (height / ( lineHeight+lineSpacing)) + 1;
- checkBufferLines();
- renumberLines();
- applySyntaxFormatting();
-
- }
-
- if(multiLine && lineNumbersEnabled) {
- lineNumberBg->setShapeSize(decoratorOffset, height);
- }
- if(scrollContainer) {
- scrollContainer->Resize(width, height);
- }
- }
- int UITextInput::insertLine(bool after) {
-
- numLines++;
-
- if(after) {
- String newText = "";
- if(lines.size() > 0) {
- String ctext = lines[lineOffset];
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- lines[lineOffset] = ctext;
- newText = text2;
- caretPosition=0;
- }
-
- vector<String>::iterator it;
- lineOffset = lineOffset + 1;
- if(lineOffset >= lines.size()) {
- it = lines.end();
- } else {
- it = lines.begin() + lineOffset;
- }
- lines.insert(it,newText);
-
- renumberLines();
- restructLines();
- } else {
- // do we even need that? I don't think so.
- }
-
- changedText();
- return 1;
- }
- void UITextInput::enableLineNumbers(bool val) {
- lineNumbersEnabled = val;
- lineNumberBg->visible = lineNumbersEnabled;
- restructLines();
- }
- void UITextInput::renumberLines() {
- if(!multiLine)
- return;
-
- int totalLineNumber = lines.size();
- if(currentBufferLines > lines.size()) {
- totalLineNumber = currentBufferLines;
- }
-
- decoratorOffset = 0;
- if(multiLine) {
- if(lineNumbersEnabled) {
- decoratorOffset = 15;
- if(totalLineNumber > 9) {
- decoratorOffset = 25;
- }
- if(totalLineNumber > 99) {
- decoratorOffset = 35;
- }
- if(totalLineNumber > 999) {
- decoratorOffset = 45;
- }
- if(totalLineNumber > 9999) {
- decoratorOffset = 55;
- }
- }
- }
-
- lineNumberAnchor->setPositionX(padding+decoratorOffset - 10);
- // Update the position and width of the text accordingly.
- textContainer->setPosition(decoratorOffset + padding, padding);
- textContainer->setWidth(this->getWidth() - textContainer->getPosition2D().x - padding);
- textContainer->setHeight(this->getHeight() - textContainer->getPosition2D().y - padding);
- textContainer->scissorBox.setRect(textContainer->getPosition2D().x, textContainer->getPosition2D().y, textContainer->getWidth(), textContainer->getHeight()+padding);
- }
- void UITextInput::restructLines() {
- for(int i=0; i < bufferLines.size(); i++) {
- bufferLines[i]->setPosition(0, (i*(lineHeight+lineSpacing)),0.0f);
- }
-
- if(multiLine && lineNumbersEnabled) {
- lineNumberBg->setShapeSize(decoratorOffset, height);
- }
-
- if(scrollContainer) {
- scrollContainer->setContentSize(width, (((lines.size()+1) * ((lineHeight+lineSpacing)))) + padding);
- }
-
- if(multiLine) {
- inputRect->setHitbox(width - scrollContainer->getVScrollWidth(), height);
- }
-
- }
- void UITextInput::setText(String text, bool sendChangeEvent) {
- if(!multiLine) {
- lines[lineOffset] = text;
- caretPosition = text.length();
- clearSelection();
- updateCaretPosition();
- } else {
- selectAll();
- insertText(text);
- clearSelection();
- }
- // needFullRedraw = true;
- changedText(sendChangeEvent);
- }
- void UITextInput::onLoseFocus() {
- blinkerRect->visible = false;
- clearSelection();
- }
- String UITextInput::getText() {
-
- if(!multiLine) {
- return lines[0];
- } else {
- String totalText = L"";
- for(int i=0; i < lines.size(); i++) {
- totalText += lines[i];
- if(i < lines.size()-1)
- totalText += L"\n";
- }
- return totalText;
- }
- }
- void UITextInput::updateCaretPosition() {
- // If this assertion fails, you likely called the function in the wrong
- // place in the constructor.
- assert(bufferLines.size() && lines.size());
- if(lineOffset > lines.size()-1)
- lineOffset = lines.size()-1;
- caretImagePosition = 0;
- if(caretPosition == 0) {
- caretImagePosition = 0;
- } else if(caretPosition > lines[lineOffset].length()) {
- caretPosition = lines[lineOffset].length();
- String caretSubString = lines[lineOffset].substr(0,caretPosition);
- caretImagePosition = bufferLines[0]->getLabel()->getTextWidthForString(caretSubString);
- } else {
- String caretSubString = lines[lineOffset].substr(0,caretPosition);
- caretImagePosition = bufferLines[0]->getLabel()->getTextWidthForString(caretSubString);
- }
- blinkerRect->visible = true;
- blinkTimer->Reset();
-
- if(doSelectToCaret) {
- doSelectToCaret = false;
- }
- if(!multiLine) {
- // Make sure the new caret position is visible.
- // Try scrolling left.
- while(caretImagePosition <= horizontalPixelScroll && horizontalCharacterScroll > 0) {
- horizontalCharacterScroll--;
- // Update pixel scroll from new character scroll.
- String subString = lines[0].substr(0,horizontalCharacterScroll);
- horizontalPixelScroll = bufferLines[0]->getLabel()->getTextWidthForString(subString);
- }
- // Try scrolling right.
- while(caretImagePosition + blinkerRect->getWidth() > horizontalPixelScroll + textContainer->getWidth()) {
- horizontalCharacterScroll++;
- // Update pixel scroll from new character scroll.
- String subString = lines[0].substr(0,horizontalCharacterScroll);
- horizontalPixelScroll = bufferLines[0]->getLabel()->getTextWidthForString(subString);
- }
- bufferLines[0]->setPosition(-horizontalPixelScroll, 0);
- }
- /*
- if(multiLine) {
- if(linesContainer->getPosition().y + currentLine->getPosition2D().y < 0.0) {
- scrollContainer->scrollVertical(-(lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
- } else if(linesContainer->getPosition().y + currentLine->getPosition2D().y > scrollContainer->getHeight()) {
- scrollContainer->scrollVertical((lineHeight+lineSpacing+padding)/(scrollContainer->getContentSize().y));
- }
- }
- */
- }
- void UITextInput::selectLineFromOffset() {
- lineOffset = lineOffset;
- }
- void UITextInput::dragSelectionTo(Number x, Number y) {
- x -= (padding * 2.0) + decoratorOffset;
- y -= padding;
- int lineOffset = y / (lineHeight+lineSpacing);
- if(lineOffset > lines.size()-1)
- lineOffset = lines.size()-1;
-
- String selectToLine = lines[lineOffset];
-
- int len = selectToLine.length();
- Number slen = 0;
- int caretPosition = bufferLines[0]->getLabel()->getTextWidthForString(selectToLine.substr(0,len)) - horizontalPixelScroll;
- for(int i=0; i < len; i++) {
- slen = bufferLines[0]->getLabel()->getTextWidthForString(selectToLine.substr(0,i)) - horizontalPixelScroll;
- if(slen > x) {
- caretPosition = i;
- break;
- }
- }
- if(x > slen)
- caretPosition = len;
-
- // if(multiLine)
- // caretPosition++;
-
- if(caretPosition < 0)
- caretPosition = 0;
- setSelection(this->lineOffset, lineOffset, this->caretPosition, caretPosition);
- }
- int UITextInput::caretSkipWordBack(int caretLine, int caretPosition) {
- for(int i=caretPosition; i > 0; i--) {
- String bit = lines[caretLine].substr(i,1);
- char chr = ((char*)bit.c_str())[0];
- if(!isNumberOrCharacter(chr) && i < caretPosition-1) {
- return i+1;
- }
- }
- return 0;
- }
- int UITextInput::caretSkipWordForward(int caretLine, int caretPosition) {
- int len = lines[caretLine].length();
- for(int i=caretPosition; i < len; i++) {
- String bit = lines[caretLine].substr(i,1);
- char chr = ((char*)bit.c_str())[0];
- if(!isNumberOrCharacter(chr) && i > caretPosition) {
- return i;
- }
- }
- return lines[caretLine].length();
- }
- void UITextInput::selectWordAtCaret() {
- caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
- clearSelection();
- updateCaretPosition();
-
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
- updateCaretPosition();
- }
- void UITextInput::replaceAll(String what, String withWhat) {
- for(int i=0; i < lines.size(); i++) {
- lines[i] = lines[i].replace(what, withWhat);
- }
- needFullRedraw = true;
- changedText();
- }
- void UITextInput::findString(String stringToFind, bool replace, String replaceString) {
- clearSelection();
- findMatches.clear();
-
- for(int i=0; i < lines.size(); i++) {
- String lineText = lines[i];
-
- int offset = 0;
- int retVal = -1;
- do {
- retVal = lineText.find(stringToFind, offset);
- if(retVal != -1) {
- FindMatch match;
- match.lineNumber = i;
- match.caretStart = retVal;
- match.caretEnd = retVal + stringToFind.length();
- findMatches.push_back(match);
- offset = retVal + stringToFind.length();
- }
- } while(retVal != -1);
-
- }
-
-
- if(findMatches.size() > 0) {
- if(replace) {
- FindMatch match = findMatches[findIndex];
- String oldText = lines[match.lineNumber];
- String newText = oldText.substr(0,match.caretStart) + replaceString + oldText.substr(match.caretEnd);
-
- lines[match.lineNumber] = newText;
- findMatches[findIndex].caretEnd = findMatches[findIndex].caretStart + replaceString.length();
- changedText();
- }
-
- findIndex = 0;
- findCurrent();
- }
- }
- void UITextInput::findNext() {
- if(findMatches.size() == 0)
- return;
- findIndex++;
- if(findIndex == findMatches.size()) {
- findIndex = 0;
- }
- findCurrent();
- }
- void UITextInput::findPrevious() {
- if(findMatches.size() == 0)
- return;
- findIndex--;
- if(findIndex < 0) {
- findIndex = findMatches.size()-1;
- }
- findCurrent();
- }
- void UITextInput::findCurrent() {
- if(findMatches.size() == 0)
- return;
- FindMatch match = findMatches[findIndex];
- lineOffset = match.lineNumber;
- caretPosition = match.caretStart;
- lineOffset = match.lineNumber;
- updateCaretPosition();
- showLine(findMatches[findIndex].lineNumber, false);
-
- setSelection(match.lineNumber, match.lineNumber, match.caretStart, match.caretEnd);
- }
- void UITextInput::setCaretToMouse(Number x, Number y) {
- clearSelection();
- x -= (padding) + decoratorOffset;
- y -= padding;
- //if(lines.size() > 1) {
- lineOffset = y / (lineHeight+lineSpacing);
- if(lineOffset > lines.size()-1)
- lineOffset = lines.size()-1;
- selectLineFromOffset();
- //}
-
- int len = lines[lineOffset].length();
- Number slen= 0;
-
- int newCaretPosition = -1;
-
- for(int i=1; i < len; i++) {
- slen = bufferLines[0]->getLabel()->getTextWidthForString(lines[lineOffset].substr(0,i)) - horizontalPixelScroll;
- Number slen_prev = bufferLines[0]->getLabel()->getTextWidthForString(lines[lineOffset].substr(0,i-1)) - horizontalPixelScroll;
- if(x >= slen_prev && x <= slen) {
- if(x < slen_prev + ((slen - slen_prev) /2.0)) {
- newCaretPosition = i-1;
- break;
- } else {
- newCaretPosition = i;
- break;
- }
- }
- }
-
- if(newCaretPosition == -1)
- newCaretPosition = 0;
-
- if(x > slen)
- newCaretPosition = len;
-
-
- caretPosition = newCaretPosition;
-
- updateCaretPosition();
- }
- void UITextInput::removeLines(unsigned int startIndex, unsigned int endIndex) {
- lines.erase(lines.begin()+startIndex, lines.begin()+endIndex);
- renumberLines();
- restructLines();
- changedText();
- }
- void UITextInput::selectAll() {
- setSelection(0, lines.size()-1, 0, lines[lines.size()-1].length());
- }
- void UITextInput::insertText(String text) {
- vector<String> strings = text.split("\n");
- settingText = true;
- if(hasSelection)
- deleteSelection();
- if(strings.size() > 1) {
- String ctext = lines[lineOffset];
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- ctext += strings[0];
- lines[lineOffset] = ctext;
- caretPosition = ctext.length();
-
- for(int i=1; i < strings.size()-1; i++) {
- insertLine(true);
- ctext = strings[i];
- lines[lineOffset] = ctext;
- caretPosition = ctext.length();
- }
-
- insertLine(true);
- ctext = strings[strings.size()-1] + text2;
- caretPosition = ctext.length();
- lines[lineOffset] = ctext;
-
- } else {
- String ctext = lines[lineOffset];
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- ctext += text + text2;
- caretPosition += text.length();
- lines[lineOffset] = ctext;
- }
-
- settingText = false;
- restructLines();
- renumberLines();
- changedText();
- updateCaretPosition();
-
- }
- String UITextInput::getLineText(unsigned int index) {
- if(index < lines.size()) {
- return lines[index];
- } else {
- return "";
- }
- }
- String UITextInput::getSelectionText() {
- if(!hasSelection)
- return L"";
-
- String totalText = L"";
- // Set up iteration cursors
- int currentLine = selectionTop;
- int currentLeft = selectionL;
- // Iterate over the inner lines(we'll be appending \n to these)
- while(currentLine < selectionBottom) {
- totalText += lines[currentLine].substr(currentLeft, lines[currentLine].length()-currentLeft) + '\n';
- currentLine++;
- currentLeft = 0;
- }
- // Add the selection in the last line(no \n needed)
- totalText += lines[currentLine].substr(currentLeft, selectionR-currentLeft);
- return totalText;
- }
- void UITextInput::setSelectionColor(Color color) {
- selectorRectTop->color = color;
- selectorRectMiddle->color = color;
- selectorRectBottom->color = color;
- }
- void UITextInput::setCursorColor(Color color) {
- blinkerRect->color = color;
- }
- void UITextInput::setBackgroundColor(Color color) {
- inputRect->color = color;
- }
- void UITextInput::setLineNumberColor(Color color) {
- lineNumberColor = color;
- for(int i=0; i < numberLines.size(); i++) {
- numberLines[i]->color = lineNumberColor;
- }
- }
- void UITextInput::setTextColor(Color color) {
- textColor = color;
- for(int i=0; i < bufferLines.size(); i++) {
- bufferLines[i]->color = textColor;
- }
- }
- UIScrollContainer *UITextInput::getScrollContainer() {
- return scrollContainer;
- }
- void UITextInput::saveUndoState() {
- UITextInputUndoState newState;
- newState.content = getText();
- newState.caretPosition = caretPosition;
- newState.lineOffset = lineOffset;
- newState.hasSelection = hasSelection;
- if(hasSelection) {
- newState.selectionLine = selectionLine;
- newState.selectionCaretPosition = selectionCaretPosition;
- }
- undoStates[undoStateIndex] = newState;
-
- // if we hit undo state capacity, shift the whole stack
- if(undoStateIndex == MAX_TEXTINPUT_UNDO_STATES-1) {
- for(int i=0; i < MAX_TEXTINPUT_UNDO_STATES-1; i++) {
- undoStates[i] = undoStates[i+1];
- }
- } else {
- undoStateIndex++;
- }
-
- maxRedoIndex = undoStateIndex;
- // By default, reset the isTypingWord status.
- // If we are typing a word after all, the caller
- // will immediately reset it to 1.
- isTypingWord = 0;
- }
- void UITextInput::setUndoState(UITextInputUndoState state) {
- clearSelection();
- setText(state.content);
- lineOffset = state.lineOffset;
- caretPosition = state.caretPosition;
- lineOffset = state.lineOffset;
- updateCaretPosition();
-
- if(state.hasSelection) {
- setSelection(lineOffset, state.selectionLine, caretPosition, state.selectionCaretPosition);
- }
-
- showLine(state.lineOffset, false);
- }
- void UITextInput::Undo() {
- if(undoStateIndex > 0) {
- undoStateIndex--;
- setUndoState(undoStates[undoStateIndex]);
- }
- }
- void UITextInput::Redo() {
- if(undoStateIndex < MAX_TEXTINPUT_UNDO_STATES-1 && undoStateIndex < maxRedoIndex) {
- undoStateIndex++;
- setUndoState(undoStates[undoStateIndex]);
- }
- }
- void UITextInput::Cut() {
- saveUndoState();
- Copy();
- if(hasSelection) {
- deleteSelection();
- } else if (getLineText(lineOffset) != "") {
- if (!multiLine) { setText(""); }
- else {
- removeLines(lineOffset, lineOffset+1);
- caretPosition = 0;
- updateCaretPosition();
- }
- }
- }
- void UITextInput::Copy() {
- if(hasSelection) {
- CoreServices::getInstance()->getCore()->copyStringToClipboard(getSelectionText());
- } else {
- if (getLineText(lineOffset) != "") {
- CoreServices::getInstance()->getCore()->copyStringToClipboard(getLineText(lineOffset));
- }
- }
- }
- void UITextInput::Paste() {
- saveUndoState();
- insertText(CoreServices::getInstance()->getCore()->getClipboardString());
- }
- void UITextInput::showLine(unsigned int lineNumber, bool top) {
- // If there's no scroll container, no need to adjust the line.
- if(!multiLine) {
- return;
- }
- if(top) {
- scrollContainer->setScrollValue(0.0, ((((lineNumber) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
- } else {
- scrollContainer->setScrollValue(0.0, (((((lineNumber) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()/2.0))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
- }
- }
- bool UITextInput::isNumberOrCharacter(wchar_t charCode) {
- if(charCode > 47 && charCode < 58)
- return true;
- if(charCode > 64 && charCode < 91)
- return true;
- if(charCode > 96 && charCode < 123)
- return true;
- return false;
- }
- void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
-
- if(!hasFocus)
- return;
-
- // Logger::log("UCHAR: %d\n", charCode);
-
- CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
-
- if(key == KEY_LEFT) {
- if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, 0);
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, 0);
- }
- } else {
- caretPosition = 0;
- clearSelection();
- updateCaretPosition();
- }
- } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordBack(selectionLine, selectionCaretPosition));
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordBack(this->lineOffset, caretPosition));
- }
- } else {
- caretPosition = caretSkipWordBack(this->lineOffset,caretPosition);
- clearSelection();
- updateCaretPosition();
- }
- } else {
- if(caretPosition > 0 || lineOffset > 0) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- // Holding down shift allows you to select with the arrow keys.
- if(hasSelection) {
- if(selectionCaretPosition > 0)
- setSelection(lineOffset, selectionLine, this->caretPosition, selectionCaretPosition-1);
- } else {
- setSelection(lineOffset, lineOffset, caretPosition, max(caretPosition-1, 0));
- }
- } else {
- int newLineStart = lineOffset;
- int newCaretPosition = caretPosition;
- if(newCaretPosition > 0) {
- newCaretPosition--;
- } else if(newLineStart > 0) {
- newLineStart--;
- newCaretPosition = lines[newLineStart].length();
- }
- clearSelection();
- caretPosition = newCaretPosition;
- lineOffset = newLineStart;
- updateCaretPosition();
- }
- }
- }
- return;
- }
-
- if(key == KEY_RIGHT) {
- if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
- if(caretPosition < lines[lineOffset].length()) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, lines[selectionLine].length());
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, lines[lineOffset].length());
- }
- } else {
- caretPosition = lines[lineOffset].length();
- clearSelection();
- updateCaretPosition();
- }
- }
- } else if (input->getKeyState(KEY_LALT) || input->getKeyState(KEY_RALT)) {
- if(caretPosition < lines[lineOffset].length()) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, caretSkipWordForward(selectionLine, selectionCaretPosition));
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretSkipWordForward(this->lineOffset, caretPosition));
- }
- } else {
- caretPosition = caretSkipWordForward(this->lineOffset,caretPosition);
- clearSelection();
- updateCaretPosition();
- }
- }
- } else {
- if(caretPosition < lines[lineOffset].length() || lineOffset + 1 < lines.size()) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- // Holding down shift allows you to select with the arrow keys.
- if(hasSelection) {
- setSelection(this->lineOffset, selectionLine, this->caretPosition, selectionCaretPosition+1);
- } else {
- setSelection(this->lineOffset, this->lineOffset, this->caretPosition, caretPosition+1);
- }
- } else {
- clearSelection();
- int newLineEnd = lineOffset;
- int newCaretPosition = caretPosition;
- if(newCaretPosition < lines[lineOffset].length()) {
- newCaretPosition++;
- } else if(newLineEnd + 1 < lines.size()) {
- newLineEnd++;
- newCaretPosition = 0;
- }
- caretPosition = newCaretPosition;
- lineOffset = newLineEnd;
- }
- updateCaretPosition();
- }
- }
- return;
- }
-
- if(key == KEY_PAGEUP) {
- if(multiLine) {
- scrollContainer->scrollVertical(-(scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
-
- }
- return;
- }
- if(key == KEY_PAGEDOWN) {
- if(multiLine) {
- scrollContainer->scrollVertical((scrollContainer->getHeight())/(scrollContainer->getContentSize().y));
-
- }
- return;
- }
-
- if(key == KEY_UP) {
- if(multiLine) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- if(selectionLine > 0)
- setSelection(this->lineOffset, selectionLine-1, this->caretPosition, selectionCaretPosition);
- } else {
- if(this->lineOffset > 0)
- setSelection(this->lineOffset, this->lineOffset-1, this->caretPosition, caretPosition);
- }
- } else {
- clearSelection();
- if(lineOffset > 0) {
- lineOffset--;
- selectLineFromOffset();
- updateCaretPosition();
- }
- }
- if(linesContainer->getPosition().y + (lineOffset*(lineHeight+lineSpacing)+padding) < 0.0) {
- scrollContainer->setScrollValue(0.0, ((((lineOffset) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
- }
- }
- blinkerRect->visible = true;
- return;
- }
-
- if(key == KEY_DOWN) {
- if(multiLine) {
- if(input->getKeyState(KEY_LSHIFT) || input->getKeyState(KEY_RSHIFT)) {
- if(hasSelection) {
- if(selectionLine < lines.size()-1)
- setSelection(this->lineOffset, selectionLine+1, this->caretPosition, selectionCaretPosition);
- } else {
- if(this->lineOffset < lines.size()-1)
- setSelection(this->lineOffset, this->lineOffset+1, this->caretPosition, caretPosition);
- }
- } else {
- clearSelection();
- if(lineOffset < lines.size()-1) {
- lineOffset++;
- selectLineFromOffset();
- updateCaretPosition();
- }
- }
- if(linesContainer->getPosition().y + (lineOffset*(lineHeight+lineSpacing)+padding) > scrollContainer->getHeight()-lineHeight-lineSpacing) {
- scrollContainer->setScrollValue(0.0, (((((lineOffset) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()-lineHeight-lineSpacing))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
- }
- }
- blinkerRect->visible = true;
- return;
- }
-
- if(key == KEY_ESCAPE) {
- if(!multiLine) {
- dispatchEvent(new Event(), Event::CANCEL_EVENT);
- }
- }
-
- if(key == KEY_RETURN) {
- if(multiLine) {
- saveUndoState();
- if(hasSelection) {
- deleteSelection();
- }
- insertLine(true);
- updateCaretPosition();
- if(linesContainer->getPosition().y + (lineOffset*(lineHeight+lineSpacing)+padding) > scrollContainer->getHeight()-lineHeight-lineSpacing) {
- scrollContainer->setScrollValue(0.0, (((((lineOffset) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()-lineHeight-lineSpacing))/(scrollContainer->getContentSize().y-scrollContainer->getHeight())));
- }
- } else {
- dispatchEvent(new Event(), Event::COMPLETE_EVENT);
- }
- return;
- }
-
- String ctext = lines[lineOffset];
-
- bool _changedText = false;
-
- if((charCode > 31 && charCode < 127) || charCode > 127) {
-
- // indent/shift text
- if (multiLine && (key == KEY_LEFTBRACKET || key == KEY_RIGHTBRACKET) &&
- (input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER) ||
- input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))) {
- shiftText( (key == KEY_RIGHTBRACKET) ? false : true );
- return;
- }
-
- else {
-
- if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
- if(!isNumberOrCharacter(charCode)) {
- saveUndoState();
- } else if (!isTypingWord) {
- saveUndoState();
- isTypingWord = 1;
- }
- if(hasSelection)
- deleteSelection();
- ctext = lines[lineOffset];
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- ctext += charCode + text2;
- caretPosition++;
- _changedText = true;
- }
-
- }
- }
-
- if(key == KEY_TAB && multiLine) {
- saveUndoState();
- if(hasSelection)
- deleteSelection();
- ctext = lines[lineOffset];
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition);
- ctext += (wchar_t)'\t' + text2;
- caretPosition++;
- _changedText = true;
- }
-
- if(key == KEY_BACKSPACE) {
- if(hasSelection) {
- saveUndoState();
- deleteSelection();
- return;
- } else {
- ctext = lines[lineOffset];
- if(caretPosition > 0) {
- saveUndoState();
- if(ctext.length() > 0) {
- String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
- ctext = ctext.substr(0,caretPosition-1);
- ctext += text2;
- caretPosition--;
- _changedText = true;
- }
- } else {
- if(lineOffset > 0) {
- saveUndoState();
- lineOffset--;
- selectLineFromOffset();
- caretPosition = lines[lineOffset].length();
- updateCaretPosition();
- lines[lineOffset] = lines[lineOffset] + ctext;
- removeLines(lineOffset+1, lineOffset+2);
- return;
- }
- }
- }
- if (multiLine) {
- if(linesContainer->getPosition().y + (lineOffset*(lineHeight+lineSpacing)+padding) < 0.0) {
- scrollContainer->setScrollValue(0.0, ((((lineOffset) * ((lineHeight+lineSpacing)))) + padding)/(scrollContainer->getContentSize().y-scrollContainer->getHeight()));
- }
- }
- }
-
- lines[lineOffset] = ctext;
-
- if(_changedText) {
- changedText();
- }
- updateCaretPosition();
- }
- void UITextInput::Update() {
- if(hasSelection) {
- blinkerRect->visible = false;
- }
- blinkerRect->setPosition(caretImagePosition + 1 -horizontalPixelScroll, (lineOffset * ( lineHeight+lineSpacing)));
- if(hasFocus) {
- // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.25f);
- } else {
- blinkerRect->visible = false;
- // inputRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
- }
-
- for(int i=0; i < linesToDelete.size(); i++) {
- delete linesToDelete[i];
- }
- linesToDelete.clear();
- textContainer->scissorBox.setRect(textContainer->getScreenPosition().x, textContainer->getScreenPosition().y, textContainer->getWidth(), textContainer->getHeight() + padding);
- }
- UITextInput::~UITextInput() {
- core->removeAllHandlersForListener(this);
- delete blinkTimer;
- linesContainer->ownsChildren = true;
- if(!ownsChildren) {
- delete linesContainer;
- delete inputRect;
- delete lineNumberBg;
- }
- }
- void UITextInput::readjustBuffer() {
- int bufferOffset = -linesContainer->position.y/ ( lineHeight+lineSpacing);
- Number bufferLineOffset = bufferOffset * ( lineHeight+lineSpacing);
-
- for(int i=0; i < bufferLines.size(); i++) {
- bufferLines[i]->getLabel()->clearColors();
-
- if(bufferOffset + i < lines.size()) {
- if(bufferOffset+i < lineColors.size()) {
- for(int j=0; j < lineColors[bufferOffset+i].colors.size(); j++) {
- bufferLines[i]->getLabel()->setColorForRange(lineColors[bufferOffset+i].colors[j].color, lineColors[bufferOffset+i].colors[j].rangeStart, lineColors[bufferOffset+i].colors[j].rangeEnd);
- bufferLines[i]->setColor(1.0, 1.0, 1.0, 1.0);
- }
- }
-
- bufferLines[i]->setText(lines[bufferOffset+i]);
- } else {
- bufferLines[i]->setText("");
- }
- bufferLines[i]->setPosition(-horizontalPixelScroll, bufferLineOffset + (i*(lineHeight+lineSpacing)),0.0f);
- }
-
- for(int i=0; i < numberLines.size(); i++) {
- if(lineNumbersEnabled) {
- numberLines[i]->setText(String::IntToString(bufferOffset+i+1));
- int textWidth = ceil(numberLines[i]->getLabel()->getTextWidth());
- numberLines[i]->setPosition(-textWidth,padding + bufferLineOffset + (i*(lineHeight+lineSpacing)),0.0f);
- numberLines[i]->visible = true;
- }
- }
-
- }
-
- void UITextInput::handleEvent(Event *event) {
- if(event->getDispatcher() == core && hasFocus) {
- switch(event->getEventCode()) {
- case Core::EVENT_UNDO:
- Undo();
- break;
- case Core::EVENT_REDO:
- Redo();
- break;
- case Core::EVENT_COPY:
- Copy();
- break;
- case Core::EVENT_CUT:
- Cut();
- break;
- case Core::EVENT_PASTE:
- Paste();
- break;
- case Core::EVENT_SELECT_ALL:
- selectAll();
- break;
-
- }
- }
- if(event->getDispatcher() == scrollContainer) {
- if(event->getEventCode() == Event::CHANGE_EVENT) {
- applySyntaxFormatting();
- }
- }
- if(event->getDispatcher() == inputRect) {
- switch(event->getEventCode()) {
- case InputEvent::EVENT_MOUSEDOWN:
- if(parentEntity) {
- ((ScreenEntity*)parentEntity)->focusChild(this);
- } else {
- hasFocus = true;
- }
- setCaretToMouse(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
- draggingSelection = true;
- break;
- case InputEvent::EVENT_MOUSEUP:
- draggingSelection = false;
- break;
- case InputEvent::EVENT_DOUBLECLICK:
- selectWordAtCaret();
- break;
- case InputEvent::EVENT_MOUSEMOVE:
- CoreServices::getInstance()->getCore()->setCursor(Core::CURSOR_TEXT);
- if(draggingSelection) {
- dragSelectionTo(((InputEvent*)event)->mousePosition.x, ((InputEvent*)event)->mousePosition.y - linesContainer->getPosition().y);
- }
- break;
- case InputEvent::EVENT_MOUSEOVER:
- CoreServices::getInstance()->getCore()->setCursor(Core::CURSOR_TEXT);
- break;
- case InputEvent::EVENT_MOUSEOUT:
- CoreServices::getInstance()->getCore()->setCursor(Core::CURSOR_ARROW);
- break;
- }
- }
-
- if(event->getDispatcher() == blinkTimer) {
- if(hasSelection || draggingSelection) {
- blinkerRect->visible = false;
- } else {
- if(hasFocus)
- blinkerRect->visible = !blinkerRect->visible;
- else
- blinkerRect->visible = false;
- }
- }
-
- }
- void UITextInput::shiftText(bool left) {
- if (multiLine && (hasSelection || lines[lineOffset] != "")) {
- saveUndoState();
-
- String t = (wchar_t)'\t';
-
- if (hasSelection) {
- for (int i = selectionTop; i <= selectionBottom; i++) {
- if (i == selectionBottom && selectionCaretPosition <= 0)
- // at least one character of bottom line needs to be selected before indenting, so...
- break;
- if (indentType == INDENT_TAB) {
- if (left) {
- if (lines[i].substr(0,1) == t) {
- lines[i] = lines[i].substr(1, lines[i].length()-1);
- caretPosition--;
- }
- } else {
- lines[i] = t + lines[i];
- caretPosition++;
- }
- } else if (indentType == INDENT_SPACE) {
- if (left) {
- // TODO
- } else {
- // TODO
- }
- }
- }
- }
- else {
- if (indentType == INDENT_TAB) {
- if (left) {
- if (lines[lineOffset].substr(0,1) == t) {
- lines[lineOffset] = lines[lineOffset].substr(1, lines[lineOffset].length()-1);
- caretPosition--;
- }
- } else {
- lines[lineOffset] = t + lines[lineOffset];
- caretPosition++;
- }
- } else if (indentType == INDENT_SPACE) {
- if (left) {
- // TODO
- } else {
- // TODO
- }
- }
- }
-
- changedText();
- updateCaretPosition();
- }
- }
- void UITextInput::convertIndentToSpaces() {
- if (indentType == INDENT_TAB) {
- indentType = INDENT_SPACE;
-
- //TODO
- }
- }
- void UITextInput::convertIndentToTabs() {
- if (indentType == INDENT_SPACE) {
- indentType = INDENT_TAB;
-
- //TODO
- }
- }
|