| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /*
- 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.
- */
- #pragma once
- #include "PolyGlobals.h"
- #include "PolyScreenLabel.h"
- #include "PolyScreenShape.h"
- #include "PolyFontManager.h"
- #include "PolyFont.h"
- #include "PolyScreenEntity.h"
- #include "PolyUIEvent.h"
- #include "PolyUIBox.h"
- #include "PolyUIElement.h"
- #include "PolyTimer.h"
- #include "PolyCoreInput.h"
- #include "PolyCore.h"
- #include <vector>
- #include "PolyUIScrollContainer.h"
- using namespace std;
- #define MAX_TEXTINPUT_UNDO_STATES 30
- namespace Polycode {
- class UITextInputUndoState {
- public:
- String content;
- unsigned int lineOffset;
- unsigned int caretPosition;
- bool hasSelection;
- int selectionLine;
- int selectionCaretPosition;
- };
-
- class _PolyExport SyntaxHighlightToken {
- public:
- SyntaxHighlightToken(String text, int type) { this->text = text; this->type = type; }
- Color color;
- String text;
- unsigned int type;
- };
-
- class _PolyExport LineColorData {
- public:
- LineColorData(Color color, unsigned int rangeStart, unsigned int rangeEnd) {
- this->color = color;
- this->rangeStart = rangeStart;
- this->rangeEnd = rangeEnd;
- }
- Color color;
- unsigned int rangeStart;
- unsigned int rangeEnd;
- };
-
- class _PolyExport LineColorInfo {
- public:
- std::vector<LineColorData> colors;
- };
-
- class _PolyExport UITextInputSyntaxHighlighter {
- public:
- virtual std::vector<SyntaxHighlightToken> parseText(String text) = 0;
- };
- class _PolyExport FindMatch {
- public:
- unsigned int lineNumber;
- unsigned int caretStart;
- unsigned int caretEnd;
- };
- class _PolyExport UITextInput : public UIElement {
- public:
- UITextInput(bool multiLine, Number width, Number height);
- ~UITextInput();
-
- void handleEvent(Event *event);
- void Update();
-
- void setText(String text);
- String getText();
- void onLoseFocus();
-
- int insertLine(bool after);
-
- void changedText();
- void applySyntaxFormatting();
-
- void onKeyDown(PolyKEY key, wchar_t charCode);
-
- void clearSelection();
- void setSelection(int lineStart, int lineEnd, int colStart, int colEnd);
-
- void deleteSelection();
- void selectAll();
-
- void Undo();
- void Redo();
- void Cut();
- void Copy();
- void Paste();
-
- void enableLineNumbers(bool val);
-
- void setBackgroundColor(Color color);
- void setSelectionColor(Color color);
- void setCursorColor(Color color);
- void setTextColor(Color color);
- void setLineNumberColor(Color color);
-
- void checkBufferLines();
-
- void replaceAll(String what, String withWhat);
-
- void findString(String stringToFind, bool replace=false, String replaceString="");
- void findNext();
- void findPrevious();
- void findCurrent();
-
- void showLine(unsigned int lineNumber, bool top);
- void setSyntaxHighlighter(UITextInputSyntaxHighlighter *syntaxHighliter);
-
- bool isNumberOrCharacter(wchar_t charCode);
- void Resize(Number width, Number height);
-
- void setNumberOnly(bool val);
-
- String getLineText(unsigned int index);
-
- String getSelectionText();
- void insertText(String text);
-
- UIScrollContainer *getScrollContainer();
-
- bool useStrongHinting;
-
- protected:
-
- void readjustBuffer();
- std::vector<LineColorInfo> lineColors;
-
- ScreenEntity *lineNumberAnchor;
-
- void renumberLines();
-
- bool lineNumbersEnabled;
-
- Color textColor;
- Color lineNumberColor;
-
- void setUndoState(UITextInputUndoState state);
- void saveUndoState();
-
- bool isNumberOnly;
-
- int caretSkipWordBack(int caretLine, int caretPosition);
- int caretSkipWordForward(int caretLine, int caretPosition);
-
- void selectLineFromOffset();
- void updateCaretPosition();
- void setCaretToMouse(Number x, Number y);
- void dragSelectionTo(Number x, Number y);
-
- void selectWordAtCaret();
-
- void restructLines();
- void removeLines(unsigned int startIndex, unsigned int endIndex);
-
- ScreenShape *selectorRectTop;
- ScreenShape *selectorRectMiddle;
- ScreenShape *selectorRectBottom;
- int numLines;
-
- bool needFullRedraw;
-
- Number padding;
- Number lineSpacing;
-
- int selectionTop;
- int selectionBottom;
- int selectionL;
- int selectionR;
-
- ScreenShape *lineNumberBg;
-
- int decoratorOffset;
-
- bool settingText;
-
- int selectionCaretPosition;
- int selectionLine;
-
- bool draggingSelection;
- bool hasSelection;
- Number caretX,caretY;
-
- int caretPosition;
- bool doSelectToCaret;
-
- UITextInputSyntaxHighlighter *syntaxHighliter;
-
- ScreenEntity *linesContainer;
-
- vector<ScreenLabel*> linesToDelete;
-
- std::vector<FindMatch> findMatches;
- int findIndex;
-
- UITextInputUndoState undoStates[MAX_TEXTINPUT_UNDO_STATES];
- int undoStateIndex;
- int maxRedoIndex;
- bool isTypingWord;
-
- bool multiLine;
- Timer *blinkTimer;
- UIBox *inputRect;
- ScreenShape *blinkerRect;
-
- Number st;
- Number sr;
- Number sb;
- Number sl;
-
- Number caretImagePosition;
-
- int currentBufferLines;
- int neededBufferLines;
-
- UIScrollContainer *scrollContainer;
-
- String fontName;
- Number fontSize;
-
- Number lineHeight;
-
- int lineOffset;
-
- vector<String> lines;
-
- vector<ScreenLabel*> bufferLines;
- vector<ScreenLabel*> numberLines;
-
- Core *core;
-
- };
- }
|