PolyUITextInput.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 "PolySceneLabel.h"
  22. #include "PolyScenePrimitive.h"
  23. #include "PolyFontManager.h"
  24. #include "PolyFont.h"
  25. #include "PolyEntity.h"
  26. #include "PolyUIEvent.h"
  27. #include "PolyUIBox.h"
  28. #include "PolyUIMenu.h"
  29. #include "PolyUIElement.h"
  30. #include "PolyTimer.h"
  31. #include "PolyCoreInput.h"
  32. #include "PolyCore.h"
  33. #include <vector>
  34. #include "PolyUIScrollContainer.h"
  35. using namespace std;
  36. #define MAX_TEXTINPUT_UNDO_STATES 30
  37. #define UI_TEXT_INPUT_SCROLL_SPEED 70.0
  38. namespace Polycode {
  39. class UITextInputUndoState {
  40. public:
  41. String content;
  42. unsigned int lineOffset;
  43. unsigned int caretPosition;
  44. bool hasSelection;
  45. int selectionLine;
  46. int selectionCaretPosition;
  47. };
  48. class _PolyExport SyntaxHighlightToken {
  49. public:
  50. SyntaxHighlightToken() {
  51. overrideType = TOKEN_TYPE_NO_OVERRIDE;
  52. }
  53. SyntaxHighlightToken(String text, int type) { this->text = text; this->type = type; overrideType = TOKEN_TYPE_NO_OVERRIDE; }
  54. Color color;
  55. String text;
  56. int overrideType;
  57. unsigned int type;
  58. static const int TOKEN_TYPE_NO_OVERRIDE = 0;
  59. static const int TOKEN_TYPE_OVERRIDE_START = 1;
  60. static const int TOKEN_TYPE_OVERRIDE_END = 2;
  61. static const int TOKEN_TYPE_OVERRIDE_LINE = 3;
  62. };
  63. class _PolyExport LineColorData {
  64. public:
  65. LineColorData() {}
  66. LineColorData(Color color, unsigned int rangeStart, unsigned int rangeEnd) {
  67. this->color = color;
  68. this->rangeStart = rangeStart;
  69. this->rangeEnd = rangeEnd;
  70. }
  71. Color color;
  72. int rangeStart;
  73. int rangeEnd;
  74. };
  75. class _PolyExport LineColorInfo {
  76. public:
  77. std::vector<LineColorData> colors;
  78. LineColorInfo getColorInfoForRange(int start, int length);
  79. };
  80. class _PolyExport UITextInputSyntaxHighlighter {
  81. public:
  82. virtual std::vector<SyntaxHighlightToken> parseText(String text, SyntaxHighlightToken overrideToken) = 0;
  83. };
  84. class _PolyExport FindMatch {
  85. public:
  86. unsigned int lineNumber;
  87. unsigned int caretStart;
  88. unsigned int caretEnd;
  89. };
  90. class WordWrapLine {
  91. public:
  92. WordWrapLine() {
  93. lastBufferIndex = -1;
  94. dirty = true;
  95. }
  96. String text;
  97. bool isWordWrap;
  98. int actualLineNumber;
  99. int lineStart;
  100. int lastBufferIndex;
  101. LineColorInfo colorInfo;
  102. bool dirty;
  103. SyntaxHighlightToken blockOverrideToken;
  104. };
  105. class TextColorPair {
  106. public:
  107. LineColorInfo colorInfo;
  108. String text;
  109. };
  110. class LineInfo {
  111. public:
  112. LineInfo(){ wordWrapLineIndex = -1; }
  113. String text;
  114. int wordWrapLineIndex;
  115. LineColorInfo colorInfo;
  116. SyntaxHighlightToken blockOverrideToken;
  117. };
  118. /**
  119. * A text input element. Can be single- or multiline.
  120. */
  121. class _PolyExport UITextInput : public UIElement {
  122. public:
  123. /**
  124. * Create a new text input element.
  125. * @param multiLine Whether the text field should consist of a single line,
  126. * or of a multiline text editor with vertical scroll bar.
  127. * @param width The width of the element.
  128. * @param height The height of the element.
  129. */
  130. UITextInput(bool multiLine, Number width, Number height);
  131. virtual ~UITextInput();
  132. void handleEvent(Event *event);
  133. void Update();
  134. /**
  135. * Set the text contents of the input.
  136. *
  137. * If the input is single-line, insert the complete text into
  138. * the line, without taking linebreaks into account.
  139. *
  140. * If the input is multi-line, each line is inserted separately
  141. * into the text field
  142. *
  143. * @param text The new text contents.
  144. * @param sendChangeEvent If true (default), will send out an Event::CHANGE_EVENT
  145. */
  146. void setText(String text, bool sendChangeEvent = true);
  147. /**
  148. * Returns the text contents of this element.
  149. *
  150. * For single-line, returns the contents of the first line.
  151. * For multi-line, returns a string containing each line in
  152. * the text field, separated by '\n'.
  153. */
  154. String getText();
  155. void onLoseFocus();
  156. /**
  157. * Insert a linebreak after the cursor and move
  158. * the cursor to the new line.
  159. *
  160. * @param after Unused. This must be true.
  161. */
  162. int insertLine(String lineText = "");
  163. void onKeyDown(PolyKEY key, wchar_t charCode);
  164. /**
  165. * Clear the current selection.
  166. */
  167. void clearSelection();
  168. /**
  169. * Set the currentselection.
  170. *
  171. * If (lineStart, colStart) is further "right" or "down" than (lineEnd, colEnd),
  172. * the two will automatically be swapped. It's thus enough to specify the two "edges"
  173. * of the selection, without knowing which comes first.
  174. *
  175. * @param lineStart The line position of one edge of the selection.
  176. * @param colStart The column position of one edge of the selection.
  177. * @param lineEnd The line position of the other edge of the selection.
  178. * @param colEnd The column position of the other edge of the selection.
  179. */
  180. void setSelection(int lineStart, int lineEnd, int colStart, int colEnd);
  181. /**
  182. * Remove the currently selected text from the text contents.
  183. */
  184. void deleteSelection();
  185. /**
  186. * Select the entire text contents.
  187. */
  188. void selectAll();
  189. /**
  190. * Reset the text contents and selection/caret to
  191. * the last undo state.
  192. */
  193. void Undo();
  194. /**
  195. * Reset the text contents and selection/caret to
  196. * the next undo state.
  197. */
  198. void Redo();
  199. /**
  200. * Remove the current selection and copy it to the clipboard.
  201. */
  202. void Cut();
  203. /**
  204. * Copy the current selection to the clipboard.
  205. */
  206. void Copy();
  207. /**
  208. * Replace the current selection with the contents of the clipboard.
  209. */
  210. void Paste();
  211. /**
  212. * Toggle line number display for each line.
  213. * @param val true to enable, false to disable.
  214. */
  215. void enableLineNumbers(bool val);
  216. /**
  217. * Set the color of the text field background.
  218. */
  219. void setBackgroundColor(Color color);
  220. /**
  221. * Set the background color for selected text.
  222. */
  223. void setSelectionColor(Color color);
  224. /**
  225. * Set the color of the cursor.
  226. */
  227. void setCursorColor(Color color);
  228. /**
  229. * Set the foreground color of displayed text.
  230. */
  231. void setTextColor(Color color);
  232. /**
  233. * Set the foreground color of line numbers.
  234. */
  235. void setLineNumberColor(Color color);
  236. void checkBufferLines();
  237. /**
  238. * Find and replace in the text contents.
  239. *
  240. * @param what The string to find.
  241. * @param withWhat The string to replace each occurrence with.
  242. */
  243. void replaceAll(String what, String withWhat);
  244. /**
  245. * Find and optionally replace a string.
  246. *
  247. * Sets the current selection to the first result. All results will be stored as instances of FindMatch
  248. * in this->findMatches, and can later be retrieved with findNext(), findPrevious() and findCurrent().
  249. *
  250. * @param stringToFind The string to find occurrences of.
  251. * @param replace Whether to replace occurrences with something.
  252. * @param replaceString The string to replace occurrences with, only used if replace=true
  253. */
  254. void findString(String stringToFind, bool replace=false, String replaceString="");
  255. std::vector<FindMatch> getFindMatches(String stringToFind);
  256. /**
  257. * Set the current find result to the next one in the result list and select it
  258. * in the text field.
  259. */
  260. void findNext();
  261. /**
  262. * Set the current find result to the previous one in the result list and select it
  263. * in the text field.
  264. */
  265. void findPrevious();
  266. /**
  267. * Set the selection to the current result in the result list.
  268. */
  269. void findCurrent();
  270. void showLine(unsigned int lineNumber, bool top);
  271. /**
  272. * Set the syntax highlighter to use for formatting text.
  273. *
  274. * @param syntaxHighlighter The syntax highlighter instance to use.
  275. */
  276. void setSyntaxHighlighter(UITextInputSyntaxHighlighter *syntaxHighlighter);
  277. void Resize(Number width, Number height);
  278. /**
  279. * Toggles whether this input accepts only numbers.
  280. *
  281. * @param val true to only accept numbers, false otherwise.
  282. */
  283. void setNumberOnly(bool val);
  284. /**
  285. * Return the contents of a line.
  286. *
  287. * @param index The index of the line to get the contents of.
  288. * First line has index 0.
  289. */
  290. String getLineText(unsigned int index);
  291. /**
  292. * Return the currently selected text.
  293. */
  294. String getSelectionText();
  295. /**
  296. * Replace the current selection with the given text.
  297. *
  298. * @param text The string to insert.
  299. */
  300. void insertText(String text, bool updateWordWrap = true);
  301. void setCaretPosition(int position);
  302. UIScrollContainer *getScrollContainer();
  303. bool useStrongHinting;
  304. void shiftText(bool left=false);
  305. void convertIndentToTabs();
  306. void convertIndentToSpaces();
  307. void doMultilineResize();
  308. static void setMenuSingleton(UIGlobalMenu *_globalMenu);
  309. protected:
  310. static UIGlobalMenu *globalMenuSingleton;
  311. void showCurrentLineIfOffscreen();
  312. void readjustBuffer(int lineStart=0, int lineEnd=-1);
  313. void updateWordWrap(int lineStart, int lineEnd);
  314. Number resizeTimer;
  315. Entity *lineNumberAnchor;
  316. void renumberLines();
  317. bool isNumberOrCharacter(wchar_t charCode);
  318. bool lineNumbersEnabled;
  319. Color textColor;
  320. Color lineNumberColor;
  321. void setUndoState(UITextInputUndoState state);
  322. void saveUndoState();
  323. void setTextDiff(String text);
  324. bool isNumberOnly;
  325. void changedText(int lineStart, int lineEnd, bool sendChangeEvent = true);
  326. void applySyntaxFormatting(int startLine, int end);
  327. void applyTokenOverride(int lineIndex, SyntaxHighlightToken overrideToken);
  328. void setActualToCaret();
  329. void setOffsetToActual();
  330. void convertOffsetToActual(int lineOffset, int caretPosition, int *actualCaretPosition);
  331. void convertActualToOffset(int actualLineOffset, int actualCaretPosition, int *lineOffset, int *caretPosition);
  332. int caretSkipWordBack(int caretLine, int caretPosition);
  333. int caretSkipWordForward(int caretLine, int caretPosition);
  334. int lineOffsetToActualLineOffset(int lineOffset);
  335. void setActualLineOffset();
  336. void updateCaretPosition();
  337. void setCaretToMouse(Number x, Number y);
  338. void dragSelectionTo(Number x, Number y);
  339. void applyBlockOverrides();
  340. void updateSelectionRects();
  341. void selectWordAtCaret();
  342. void restructLines();
  343. void removeLines(unsigned int startIndex, unsigned int endIndex);
  344. std::vector<TextColorPair> makeWordWrapBuffer(LineInfo *lineInfo, String indentPrefix);
  345. std::vector<TextColorPair> splitTokens(String stringToSplit, LineColorInfo *stringColorInfo);
  346. UIRect *selectorRectTop;
  347. UIRect *selectorRectMiddle;
  348. UIRect *selectorRectBottom;
  349. int numLines;
  350. Number padding;
  351. Number lineSpacing;
  352. int selectionTop;
  353. int selectionBottom;
  354. int selectionL;
  355. int selectionR;
  356. UIRect *lineNumberBg;
  357. int decoratorOffset;
  358. bool settingText;
  359. int selectionCaretPosition;
  360. int selectionLine;
  361. // Used only by single-line text input, so
  362. // that you can edit text longer than the width
  363. // of the line.
  364. // TODO/considerations: Use this to scroll a single
  365. // line in multi-line mode?
  366. int horizontalPixelScroll;
  367. // By how many characters have we scrolled right?
  368. int horizontalCharacterScroll;
  369. bool draggingSelection;
  370. bool hasSelection;
  371. Number caretX,caretY;
  372. int caretPosition;
  373. int actualCaretPosition;
  374. bool doSelectToCaret;
  375. UITextInputSyntaxHighlighter *syntaxHighliter;
  376. Entity *linesContainer;
  377. // container for the actual text contents
  378. UIElement *textContainer;
  379. vector<SceneLabel*> linesToDelete;
  380. std::vector<FindMatch> findMatches;
  381. int findIndex;
  382. UITextInputUndoState undoStates[MAX_TEXTINPUT_UNDO_STATES];
  383. int undoStateIndex;
  384. int maxRedoIndex;
  385. bool isTypingWord;
  386. bool multiLine;
  387. Timer *blinkTimer;
  388. UIBox *inputRect;
  389. UIRect *blinkerRect;
  390. Vector2 dragMouseStart;
  391. Color selectionColor;
  392. void _setSelectionColor(Color color);
  393. Number st;
  394. Number sr;
  395. Number sb;
  396. Number sl;
  397. UIMenu *contextMenu;
  398. Vector2 selectionDragMouse;
  399. Number caretImagePosition;
  400. int currentBufferLines;
  401. int neededBufferLines;
  402. UIScrollContainer *scrollContainer;
  403. String fontName;
  404. Number fontSize;
  405. Number lineHeight;
  406. int lineOffset;
  407. int actualLineOffset;
  408. vector<LineInfo> lines;
  409. vector<WordWrapLine> wordWrapLines;
  410. vector<SceneLabel*> bufferLines;
  411. vector<SceneLabel*> numberLines;
  412. Core *core;
  413. Number lastResizeWidth;
  414. Number _newWidth;
  415. Number _newHeight;
  416. bool didMultilineResize;
  417. enum indentTypes { INDENT_SPACE, INDENT_TAB } indentType;
  418. int indentSpacing;
  419. };
  420. }