PolyUITextInput.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyScreenLabel.h"
  22. #include "PolyScreenShape.h"
  23. #include "PolyFontManager.h"
  24. #include "PolyFont.h"
  25. #include "PolyScreenEntity.h"
  26. #include "PolyUIEvent.h"
  27. #include "PolyUIBox.h"
  28. #include "PolyUIElement.h"
  29. #include "PolyTimer.h"
  30. #include "PolyCoreInput.h"
  31. #include "PolyCore.h"
  32. #include <vector>
  33. #include "PolyUIScrollContainer.h"
  34. using namespace std;
  35. #define MAX_TEXTINPUT_UNDO_STATES 30
  36. namespace Polycode {
  37. class UITextInputUndoState {
  38. public:
  39. String content;
  40. unsigned int lineOffset;
  41. unsigned int caretPosition;
  42. bool hasSelection;
  43. int selectionLine;
  44. int selectionCaretPosition;
  45. };
  46. class _PolyExport SyntaxHighlightToken {
  47. public:
  48. SyntaxHighlightToken(String text, int type) { this->text = text; this->type = type; }
  49. Color color;
  50. String text;
  51. unsigned int type;
  52. };
  53. class _PolyExport LineColorData {
  54. public:
  55. LineColorData(Color color, unsigned int rangeStart, unsigned int rangeEnd) {
  56. this->color = color;
  57. this->rangeStart = rangeStart;
  58. this->rangeEnd = rangeEnd;
  59. }
  60. Color color;
  61. unsigned int rangeStart;
  62. unsigned int rangeEnd;
  63. };
  64. class _PolyExport LineColorInfo {
  65. public:
  66. std::vector<LineColorData> colors;
  67. };
  68. class _PolyExport UITextInputSyntaxHighlighter {
  69. public:
  70. virtual std::vector<SyntaxHighlightToken> parseText(String text) = 0;
  71. };
  72. class _PolyExport FindMatch {
  73. public:
  74. unsigned int lineNumber;
  75. unsigned int caretStart;
  76. unsigned int caretEnd;
  77. };
  78. class _PolyExport UITextInput : public UIElement {
  79. public:
  80. UITextInput(bool multiLine, Number width, Number height);
  81. ~UITextInput();
  82. void handleEvent(Event *event);
  83. void Update();
  84. void setText(String text);
  85. String getText();
  86. void onLoseFocus();
  87. int insertLine(bool after);
  88. void changedText();
  89. void applySyntaxFormatting();
  90. void onKeyDown(PolyKEY key, wchar_t charCode);
  91. void clearSelection();
  92. void setSelection(int lineStart, int lineEnd, int colStart, int colEnd);
  93. void deleteSelection();
  94. void selectAll();
  95. void Undo();
  96. void Redo();
  97. void Cut();
  98. void Copy();
  99. void Paste();
  100. void enableLineNumbers(bool val);
  101. void setBackgroundColor(Color color);
  102. void setSelectionColor(Color color);
  103. void setCursorColor(Color color);
  104. void setTextColor(Color color);
  105. void setLineNumberColor(Color color);
  106. void checkBufferLines();
  107. void replaceAll(String what, String withWhat);
  108. void findString(String stringToFind, bool replace=false, String replaceString="");
  109. void findNext();
  110. void findPrevious();
  111. void findCurrent();
  112. void showLine(unsigned int lineNumber, bool top);
  113. void setSyntaxHighlighter(UITextInputSyntaxHighlighter *syntaxHighliter);
  114. bool isNumberOrCharacter(wchar_t charCode);
  115. void Resize(Number width, Number height);
  116. void setNumberOnly(bool val);
  117. String getLineText(unsigned int index);
  118. String getSelectionText();
  119. void insertText(String text);
  120. UIScrollContainer *getScrollContainer();
  121. bool useStrongHinting;
  122. protected:
  123. void readjustBuffer();
  124. std::vector<LineColorInfo> lineColors;
  125. ScreenEntity *lineNumberAnchor;
  126. void renumberLines();
  127. bool lineNumbersEnabled;
  128. Color textColor;
  129. Color lineNumberColor;
  130. void setUndoState(UITextInputUndoState state);
  131. void saveUndoState();
  132. bool isNumberOnly;
  133. int caretSkipWordBack(int caretLine, int caretPosition);
  134. int caretSkipWordForward(int caretLine, int caretPosition);
  135. void selectLineFromOffset();
  136. void updateCaretPosition();
  137. void setCaretToMouse(Number x, Number y);
  138. void dragSelectionTo(Number x, Number y);
  139. void selectWordAtCaret();
  140. void restructLines();
  141. void removeLines(unsigned int startIndex, unsigned int endIndex);
  142. ScreenShape *selectorRectTop;
  143. ScreenShape *selectorRectMiddle;
  144. ScreenShape *selectorRectBottom;
  145. int numLines;
  146. bool needFullRedraw;
  147. Number padding;
  148. Number lineSpacing;
  149. int selectionTop;
  150. int selectionBottom;
  151. int selectionL;
  152. int selectionR;
  153. ScreenShape *lineNumberBg;
  154. int decoratorOffset;
  155. bool settingText;
  156. int selectionCaretPosition;
  157. int selectionLine;
  158. bool draggingSelection;
  159. bool hasSelection;
  160. Number caretX,caretY;
  161. int caretPosition;
  162. bool doSelectToCaret;
  163. UITextInputSyntaxHighlighter *syntaxHighliter;
  164. ScreenEntity *linesContainer;
  165. vector<ScreenLabel*> linesToDelete;
  166. std::vector<FindMatch> findMatches;
  167. int findIndex;
  168. UITextInputUndoState undoStates[MAX_TEXTINPUT_UNDO_STATES];
  169. int undoStateIndex;
  170. int maxRedoIndex;
  171. bool isTypingWord;
  172. bool multiLine;
  173. Timer *blinkTimer;
  174. UIBox *inputRect;
  175. ScreenShape *blinkerRect;
  176. Number st;
  177. Number sr;
  178. Number sb;
  179. Number sl;
  180. Number caretImagePosition;
  181. int currentBufferLines;
  182. int neededBufferLines;
  183. UIScrollContainer *scrollContainer;
  184. String fontName;
  185. Number fontSize;
  186. Number lineHeight;
  187. int lineOffset;
  188. vector<String> lines;
  189. vector<ScreenLabel*> bufferLines;
  190. vector<ScreenLabel*> numberLines;
  191. Core *core;
  192. };
  193. }