Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

789 lines
32KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. An editable text box.
  18. A TextEditor can either be in single- or multi-line mode, and supports mixed
  19. fonts and colours.
  20. @see TextEditor::Listener, Label
  21. @tags{GUI}
  22. */
  23. class JUCE_API TextEditor : public Component,
  24. public TextInputTarget,
  25. public SettableTooltipClient
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates a new, empty text editor.
  30. @param componentName the name to pass to the component for it to use as its name
  31. @param passwordCharacter if this is not zero, this character will be used as a replacement
  32. for all characters that are drawn on screen - e.g. to create
  33. a password-style textbox containing circular blobs instead of text,
  34. you could set this value to 0x25cf, which is the unicode character
  35. for a black splodge (not all fonts include this, though), or 0x2022,
  36. which is a bullet (probably the best choice for linux).
  37. */
  38. explicit TextEditor (const String& componentName = String(),
  39. juce_wchar passwordCharacter = 0);
  40. /** Destructor. */
  41. ~TextEditor() override;
  42. //==============================================================================
  43. /** Puts the editor into either multi- or single-line mode.
  44. By default, the editor will be in single-line mode, so use this if you need a multi-line
  45. editor.
  46. See also the setReturnKeyStartsNewLine() method, which will also need to be turned
  47. on if you want a multi-line editor with line-breaks.
  48. @see isMultiLine, setReturnKeyStartsNewLine
  49. */
  50. void setMultiLine (bool shouldBeMultiLine,
  51. bool shouldWordWrap = true);
  52. /** Returns true if the editor is in multi-line mode. */
  53. bool isMultiLine() const;
  54. //==============================================================================
  55. /** Changes the behaviour of the return key.
  56. If set to true, the return key will insert a new-line into the text; if false
  57. it will trigger a call to the TextEditor::Listener::textEditorReturnKeyPressed()
  58. method. By default this is set to false, and when true it will only insert
  59. new-lines when in multi-line mode (see setMultiLine()).
  60. */
  61. void setReturnKeyStartsNewLine (bool shouldStartNewLine);
  62. /** Returns the value set by setReturnKeyStartsNewLine().
  63. See setReturnKeyStartsNewLine() for more info.
  64. */
  65. bool getReturnKeyStartsNewLine() const { return returnKeyStartsNewLine; }
  66. /** Indicates whether the tab key should be accepted and used to input a tab character,
  67. or whether it gets ignored.
  68. By default the tab key is ignored, so that it can be used to switch keyboard focus
  69. between components.
  70. */
  71. void setTabKeyUsedAsCharacter (bool shouldTabKeyBeUsed);
  72. /** Returns true if the tab key is being used for input.
  73. @see setTabKeyUsedAsCharacter
  74. */
  75. bool isTabKeyUsedAsCharacter() const { return tabKeyUsed; }
  76. /** This can be used to change whether escape and return keypress events are
  77. propagated up to the parent component.
  78. The default here is true, meaning that these events are not allowed to reach the
  79. parent, but you may want to allow them through so that they can trigger other
  80. actions, e.g. closing a dialog box, etc.
  81. */
  82. void setEscapeAndReturnKeysConsumed (bool shouldBeConsumed) noexcept;
  83. //==============================================================================
  84. /** Changes the editor to read-only mode.
  85. By default, the text editor is not read-only. If you're making it read-only, you
  86. might also want to call setCaretVisible (false) to get rid of the caret.
  87. The text can still be highlighted and copied when in read-only mode.
  88. @see isReadOnly, setCaretVisible
  89. */
  90. void setReadOnly (bool shouldBeReadOnly);
  91. /** Returns true if the editor is in read-only mode. */
  92. bool isReadOnly() const noexcept;
  93. //==============================================================================
  94. /** Makes the caret visible or invisible.
  95. By default the caret is visible.
  96. @see setCaretColour, setCaretPosition
  97. */
  98. void setCaretVisible (bool shouldBeVisible);
  99. /** Returns true if the caret is enabled.
  100. @see setCaretVisible
  101. */
  102. bool isCaretVisible() const noexcept { return caretVisible && ! isReadOnly(); }
  103. //==============================================================================
  104. /** Enables/disables a vertical scrollbar.
  105. (This only applies when in multi-line mode). When the text gets too long to fit
  106. in the component, a scrollbar can appear to allow it to be scrolled. Even when
  107. this is enabled, the scrollbar will be hidden unless it's needed.
  108. By default the scrollbar is enabled.
  109. */
  110. void setScrollbarsShown (bool shouldBeEnabled);
  111. /** Returns true if scrollbars are enabled.
  112. @see setScrollbarsShown
  113. */
  114. bool areScrollbarsShown() const noexcept { return scrollbarVisible; }
  115. /** Changes the password character used to disguise the text.
  116. @param passwordCharacter if this is not zero, this character will be used as a replacement
  117. for all characters that are drawn on screen - e.g. to create
  118. a password-style textbox containing circular blobs instead of text,
  119. you could set this value to 0x25cf, which is the unicode character
  120. for a black splodge (not all fonts include this, though), or 0x2022,
  121. which is a bullet (probably the best choice for linux).
  122. */
  123. void setPasswordCharacter (juce_wchar passwordCharacter);
  124. /** Returns the current password character.
  125. @see setPasswordCharacter
  126. */
  127. juce_wchar getPasswordCharacter() const noexcept { return passwordCharacter; }
  128. //==============================================================================
  129. /** Allows a right-click menu to appear for the editor.
  130. (This defaults to being enabled).
  131. If enabled, right-clicking (or command-clicking on the Mac) will pop up a menu
  132. of options such as cut/copy/paste, undo/redo, etc.
  133. */
  134. void setPopupMenuEnabled (bool menuEnabled);
  135. /** Returns true if the right-click menu is enabled.
  136. @see setPopupMenuEnabled
  137. */
  138. bool isPopupMenuEnabled() const noexcept { return popupMenuEnabled; }
  139. /** Returns true if a popup-menu is currently being displayed. */
  140. bool isPopupMenuCurrentlyActive() const noexcept { return menuActive; }
  141. //==============================================================================
  142. /** A set of colour IDs to use to change the colour of various aspects of the editor.
  143. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  144. methods.
  145. NB: You can also set the caret colour using CaretComponent::caretColourId
  146. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  147. */
  148. enum ColourIds
  149. {
  150. backgroundColourId = 0x1000200, /**< The colour to use for the text component's background - this can be
  151. transparent if necessary. */
  152. textColourId = 0x1000201, /**< The colour that will be used when text is added to the editor. Note
  153. that because the editor can contain multiple colours, calling this
  154. method won't change the colour of existing text - to do that, use
  155. the applyColourToAllText() method */
  156. highlightColourId = 0x1000202, /**< The colour with which to fill the background of highlighted sections of
  157. the text - this can be transparent if you don't want to show any
  158. highlighting.*/
  159. highlightedTextColourId = 0x1000203, /**< The colour with which to draw the text in highlighted sections. */
  160. outlineColourId = 0x1000205, /**< If this is non-transparent, it will be used to draw a box around
  161. the edge of the component. */
  162. focusedOutlineColourId = 0x1000206, /**< If this is non-transparent, it will be used to draw a box around
  163. the edge of the component when it has focus. */
  164. shadowColourId = 0x1000207, /**< If this is non-transparent, it'll be used to draw an inner shadow
  165. around the edge of the editor. */
  166. };
  167. //==============================================================================
  168. /** Sets the font to use for newly added text.
  169. This will change the font that will be used next time any text is added or entered
  170. into the editor. It won't change the font of any existing text - to do that, use
  171. applyFontToAllText() instead.
  172. @see applyFontToAllText
  173. */
  174. void setFont (const Font& newFont);
  175. /** Applies a font to all the text in the editor.
  176. If the changeCurrentFont argument is true then this will also set the
  177. new font as the font to be used for any new text that's added.
  178. @see setFont
  179. */
  180. void applyFontToAllText (const Font& newFont, bool changeCurrentFont = true);
  181. /** Returns the font that's currently being used for new text.
  182. @see setFont
  183. */
  184. const Font& getFont() const noexcept { return currentFont; }
  185. /** Applies a colour to all the text in the editor.
  186. If the changeCurrentTextColour argument is true then this will also set the
  187. new colour as the colour to be used for any new text that's added.
  188. */
  189. void applyColourToAllText (const Colour& newColour, bool changeCurrentTextColour = true);
  190. //==============================================================================
  191. /** If set to true, focusing on the editor will highlight all its text.
  192. (Set to false by default).
  193. This is useful for boxes where you expect the user to re-enter all the
  194. text when they focus on the component, rather than editing what's already there.
  195. */
  196. void setSelectAllWhenFocused (bool shouldSelectAll);
  197. /** When the text editor is empty, it can be set to display a message.
  198. This is handy for things like telling the user what to type in the box - the
  199. string is only displayed, it's not taken to actually be the contents of
  200. the editor.
  201. */
  202. void setTextToShowWhenEmpty (const String& text, Colour colourToUse);
  203. /** Returns the text that will be shown when the text editor is empty.
  204. @see setTextToShowWhenEmpty
  205. */
  206. String getTextToShowWhenEmpty() const noexcept { return textToShowWhenEmpty; }
  207. //==============================================================================
  208. /** Changes the size of the scrollbars that are used.
  209. Handy if you need smaller scrollbars for a small text box.
  210. */
  211. void setScrollBarThickness (int newThicknessPixels);
  212. //==============================================================================
  213. /**
  214. Receives callbacks from a TextEditor component when it changes.
  215. @see TextEditor::addListener
  216. */
  217. class JUCE_API Listener
  218. {
  219. public:
  220. /** Destructor. */
  221. virtual ~Listener() = default;
  222. /** Called when the user changes the text in some way. */
  223. virtual void textEditorTextChanged (TextEditor&) {}
  224. /** Called when the user presses the return key. */
  225. virtual void textEditorReturnKeyPressed (TextEditor&) {}
  226. /** Called when the user presses the escape key. */
  227. virtual void textEditorEscapeKeyPressed (TextEditor&) {}
  228. /** Called when the text editor loses focus. */
  229. virtual void textEditorFocusLost (TextEditor&) {}
  230. };
  231. /** Registers a listener to be told when things happen to the text.
  232. @see removeListener
  233. */
  234. void addListener (Listener* newListener);
  235. /** Deregisters a listener.
  236. @see addListener
  237. */
  238. void removeListener (Listener* listenerToRemove);
  239. //==============================================================================
  240. /** You can assign a lambda to this callback object to have it called when the text is changed. */
  241. std::function<void()> onTextChange;
  242. /** You can assign a lambda to this callback object to have it called when the return key is pressed. */
  243. std::function<void()> onReturnKey;
  244. /** You can assign a lambda to this callback object to have it called when the escape key is pressed. */
  245. std::function<void()> onEscapeKey;
  246. /** You can assign a lambda to this callback object to have it called when the editor loses key focus. */
  247. std::function<void()> onFocusLost;
  248. //==============================================================================
  249. /** Returns the entire contents of the editor. */
  250. String getText() const;
  251. /** Returns a section of the contents of the editor. */
  252. String getTextInRange (const Range<int>& textRange) const override;
  253. /** Returns true if there are no characters in the editor.
  254. This is far more efficient than calling getText().isEmpty().
  255. */
  256. bool isEmpty() const;
  257. /** Sets the entire content of the editor.
  258. This will clear the editor and insert the given text (using the current text colour
  259. and font). You can set the current text colour using
  260. @code setColour (TextEditor::textColourId, ...);
  261. @endcode
  262. @param newText the text to add
  263. @param sendTextChangeMessage if true, this will cause a change message to
  264. be sent to all the listeners.
  265. @see insertTextAtCaret
  266. */
  267. void setText (const String& newText,
  268. bool sendTextChangeMessage = true);
  269. /** Returns a Value object that can be used to get or set the text.
  270. Bear in mind that this operate quite slowly if your text box contains large
  271. amounts of text, as it needs to dynamically build the string that's involved.
  272. It's best used for small text boxes.
  273. */
  274. Value& getTextValue();
  275. /** Inserts some text at the current caret position.
  276. If a section of the text is highlighted, it will be replaced by
  277. this string, otherwise it will be inserted.
  278. To delete a section of text, you can use setHighlightedRegion() to
  279. highlight it, and call insertTextAtCaret (String()).
  280. @see setCaretPosition, getCaretPosition, setHighlightedRegion
  281. */
  282. void insertTextAtCaret (const String& textToInsert) override;
  283. /** Deletes all the text from the editor. */
  284. void clear();
  285. /** Deletes the currently selected region.
  286. This doesn't copy the deleted section to the clipboard - if you need to do that, call copy() first.
  287. @see copy, paste, SystemClipboard
  288. */
  289. void cut();
  290. /** Copies the currently selected region to the clipboard.
  291. @see cut, paste, SystemClipboard
  292. */
  293. void copy();
  294. /** Pastes the contents of the clipboard into the editor at the caret position.
  295. @see cut, copy, SystemClipboard
  296. */
  297. void paste();
  298. //==============================================================================
  299. /** Returns the current index of the caret.
  300. @see setCaretPosition
  301. */
  302. int getCaretPosition() const;
  303. /** Moves the caret to be in front of a given character.
  304. @see getCaretPosition, moveCaretToEnd
  305. */
  306. void setCaretPosition (int newIndex);
  307. /** Attempts to scroll the text editor so that the caret ends up at
  308. a specified position.
  309. This won't affect the caret's position within the text, it tries to scroll
  310. the entire editor vertically and horizontally so that the caret is sitting
  311. at the given position (relative to the top-left of this component).
  312. Depending on the amount of text available, it might not be possible to
  313. scroll far enough for the caret to reach this exact position, but it
  314. will go as far as it can in that direction.
  315. */
  316. void scrollEditorToPositionCaret (int desiredCaretX, int desiredCaretY);
  317. /** Get the graphical position of the caret.
  318. The rectangle returned is relative to the component's top-left corner.
  319. @see scrollEditorToPositionCaret
  320. */
  321. Rectangle<int> getCaretRectangle() override;
  322. /** Selects a section of the text. */
  323. void setHighlightedRegion (const Range<int>& newSelection) override;
  324. /** Returns the range of characters that are selected.
  325. If nothing is selected, this will return an empty range.
  326. @see setHighlightedRegion
  327. */
  328. Range<int> getHighlightedRegion() const override { return selection; }
  329. /** Returns the section of text that is currently selected. */
  330. String getHighlightedText() const;
  331. /** Finds the index of the character at a given position.
  332. The coordinates are relative to the component's top-left.
  333. */
  334. int getTextIndexAt (int x, int y);
  335. /** Counts the number of characters in the text.
  336. This is quicker than getting the text as a string if you just need to know
  337. the length.
  338. */
  339. int getTotalNumChars() const;
  340. /** Returns the total width of the text, as it is currently laid-out.
  341. This may be larger than the size of the TextEditor, and can change when
  342. the TextEditor is resized or the text changes.
  343. */
  344. int getTextWidth() const;
  345. /** Returns the maximum height of the text, as it is currently laid-out.
  346. This may be larger than the size of the TextEditor, and can change when
  347. the TextEditor is resized or the text changes.
  348. */
  349. int getTextHeight() const;
  350. /** Changes the size of the gap at the top and left-edge of the editor.
  351. By default there's a gap of 4 pixels.
  352. */
  353. void setIndents (int newLeftIndent, int newTopIndent);
  354. /** Changes the size of border left around the edge of the component.
  355. @see getBorder
  356. */
  357. void setBorder (BorderSize<int> border);
  358. /** Returns the size of border around the edge of the component.
  359. @see setBorder
  360. */
  361. BorderSize<int> getBorder() const;
  362. /** Used to disable the auto-scrolling which keeps the caret visible.
  363. If true (the default), the editor will scroll when the caret moves offscreen. If
  364. set to false, it won't.
  365. */
  366. void setScrollToShowCursor (bool shouldScrollToShowCaret);
  367. /** Modifies the horizontal justification of the text within the editor window. */
  368. void setJustification (Justification newJustification);
  369. /** Sets the line spacing of the TextEditor.
  370. The default (and minimum) value is 1.0 and values > 1.0 will increase the line spacing as a
  371. multiple of the line height e.g. for double-spacing call this method with an argument of 2.0.
  372. */
  373. void setLineSpacing (float newLineSpacing) noexcept { lineSpacing = jmax (1.0f, newLineSpacing); }
  374. /** Returns the current line spacing of the TextEditor. */
  375. float getLineSpacing() const noexcept { return lineSpacing; }
  376. //==============================================================================
  377. void moveCaretToEnd();
  378. bool moveCaretLeft (bool moveInWholeWordSteps, bool selecting);
  379. bool moveCaretRight (bool moveInWholeWordSteps, bool selecting);
  380. bool moveCaretUp (bool selecting);
  381. bool moveCaretDown (bool selecting);
  382. bool pageUp (bool selecting);
  383. bool pageDown (bool selecting);
  384. bool scrollDown();
  385. bool scrollUp();
  386. bool moveCaretToTop (bool selecting);
  387. bool moveCaretToStartOfLine (bool selecting);
  388. bool moveCaretToEnd (bool selecting);
  389. bool moveCaretToEndOfLine (bool selecting);
  390. bool deleteBackwards (bool moveInWholeWordSteps);
  391. bool deleteForwards (bool moveInWholeWordSteps);
  392. bool copyToClipboard();
  393. bool cutToClipboard();
  394. bool pasteFromClipboard();
  395. bool selectAll();
  396. bool undo();
  397. bool redo();
  398. //==============================================================================
  399. /** This adds the items to the popup menu.
  400. By default it adds the cut/copy/paste items, but you can override this if
  401. you need to replace these with your own items.
  402. If you want to add your own items to the existing ones, you can override this,
  403. call the base class's addPopupMenuItems() method, then append your own items.
  404. When the menu has been shown, performPopupMenuAction() will be called to
  405. perform the item that the user has chosen.
  406. The default menu items will be added using item IDs from the
  407. StandardApplicationCommandIDs namespace.
  408. If this was triggered by a mouse-click, the mouseClickEvent parameter will be
  409. a pointer to the info about it, or may be null if the menu is being triggered
  410. by some other means.
  411. @see performPopupMenuAction, setPopupMenuEnabled, isPopupMenuEnabled
  412. */
  413. virtual void addPopupMenuItems (PopupMenu& menuToAddTo,
  414. const MouseEvent* mouseClickEvent);
  415. /** This is called to perform one of the items that was shown on the popup menu.
  416. If you've overridden addPopupMenuItems(), you should also override this
  417. to perform the actions that you've added.
  418. If you've overridden addPopupMenuItems() but have still left the default items
  419. on the menu, remember to call the superclass's performPopupMenuAction()
  420. so that it can perform the default actions if that's what the user clicked on.
  421. @see addPopupMenuItems, setPopupMenuEnabled, isPopupMenuEnabled
  422. */
  423. virtual void performPopupMenuAction (int menuItemID);
  424. //==============================================================================
  425. /** Base class for input filters that can be applied to a TextEditor to restrict
  426. the text that can be entered.
  427. */
  428. class JUCE_API InputFilter
  429. {
  430. public:
  431. InputFilter() = default;
  432. virtual ~InputFilter() = default;
  433. /** This method is called whenever text is entered into the editor.
  434. An implementation of this class should should check the input string,
  435. and return an edited version of it that should be used.
  436. */
  437. virtual String filterNewText (TextEditor&, const String& newInput) = 0;
  438. };
  439. /** An input filter for a TextEditor that limits the length of text and/or the
  440. characters that it may contain.
  441. */
  442. class JUCE_API LengthAndCharacterRestriction : public InputFilter
  443. {
  444. public:
  445. /** Creates a filter that limits the length of text, and/or the characters that it can contain.
  446. @param maxNumChars if this is > 0, it sets a maximum length limit; if <= 0, no
  447. limit is set
  448. @param allowedCharacters if this is non-empty, then only characters that occur in
  449. this string are allowed to be entered into the editor.
  450. */
  451. LengthAndCharacterRestriction (int maxNumChars, const String& allowedCharacters);
  452. String filterNewText (TextEditor&, const String&) override;
  453. private:
  454. String allowedCharacters;
  455. int maxLength;
  456. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LengthAndCharacterRestriction)
  457. };
  458. /** Sets an input filter that should be applied to this editor.
  459. The filter can be nullptr, to remove any existing filters.
  460. If takeOwnership is true, then the filter will be owned and deleted by the editor
  461. when no longer needed.
  462. */
  463. void setInputFilter (InputFilter* newFilter, bool takeOwnership);
  464. /** Returns the current InputFilter, as set by setInputFilter(). */
  465. InputFilter* getInputFilter() const noexcept { return inputFilter; }
  466. /** Sets limits on the characters that can be entered.
  467. This is just a shortcut that passes an instance of the LengthAndCharacterRestriction
  468. class to setInputFilter().
  469. @param maxTextLength if this is > 0, it sets a maximum length limit; if 0, no
  470. limit is set
  471. @param allowedCharacters if this is non-empty, then only characters that occur in
  472. this string are allowed to be entered into the editor.
  473. */
  474. void setInputRestrictions (int maxTextLength,
  475. const String& allowedCharacters = String());
  476. void setKeyboardType (VirtualKeyboardType type) noexcept { keyboardType = type; }
  477. //==============================================================================
  478. /** This abstract base class is implemented by LookAndFeel classes to provide
  479. TextEditor drawing functionality.
  480. */
  481. struct JUCE_API LookAndFeelMethods
  482. {
  483. virtual ~LookAndFeelMethods() = default;
  484. virtual void fillTextEditorBackground (Graphics&, int width, int height, TextEditor&) = 0;
  485. virtual void drawTextEditorOutline (Graphics&, int width, int height, TextEditor&) = 0;
  486. virtual CaretComponent* createCaretComponent (Component* keyFocusOwner) = 0;
  487. };
  488. //==============================================================================
  489. /** @internal */
  490. void paint (Graphics&) override;
  491. /** @internal */
  492. void paintOverChildren (Graphics&) override;
  493. /** @internal */
  494. void mouseDown (const MouseEvent&) override;
  495. /** @internal */
  496. void mouseUp (const MouseEvent&) override;
  497. /** @internal */
  498. void mouseDrag (const MouseEvent&) override;
  499. /** @internal */
  500. void mouseDoubleClick (const MouseEvent&) override;
  501. /** @internal */
  502. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  503. /** @internal */
  504. bool keyPressed (const KeyPress&) override;
  505. /** @internal */
  506. bool keyStateChanged (bool) override;
  507. /** @internal */
  508. void focusGained (FocusChangeType) override;
  509. /** @internal */
  510. void focusLost (FocusChangeType) override;
  511. /** @internal */
  512. void resized() override;
  513. /** @internal */
  514. void enablementChanged() override;
  515. /** @internal */
  516. void lookAndFeelChanged() override;
  517. /** @internal */
  518. void parentHierarchyChanged() override;
  519. /** @internal */
  520. bool isTextInputActive() const override;
  521. /** @internal */
  522. void setTemporaryUnderlining (const Array<Range<int>>&) override;
  523. /** @internal */
  524. VirtualKeyboardType getKeyboardType() override { return keyboardType; }
  525. protected:
  526. //==============================================================================
  527. /** Scrolls the minimum distance needed to get the caret into view. */
  528. void scrollToMakeSureCursorIsVisible();
  529. /** Used internally to dispatch a text-change message. */
  530. void textChanged();
  531. /** Begins a new transaction in the UndoManager. */
  532. void newTransaction();
  533. /** Can be overridden to intercept return key presses directly */
  534. virtual void returnPressed();
  535. /** Can be overridden to intercept escape key presses directly */
  536. virtual void escapePressed();
  537. private:
  538. //==============================================================================
  539. JUCE_PUBLIC_IN_DLL_BUILD (class UniformTextSection)
  540. struct Iterator;
  541. struct TextHolderComponent;
  542. struct TextEditorViewport;
  543. struct InsertAction;
  544. struct RemoveAction;
  545. std::unique_ptr<Viewport> viewport;
  546. TextHolderComponent* textHolder;
  547. BorderSize<int> borderSize { 1, 1, 1, 3 };
  548. Justification justification { Justification::left };
  549. bool readOnly = false;
  550. bool caretVisible = true;
  551. bool multiline = false;
  552. bool wordWrap = false;
  553. bool returnKeyStartsNewLine = false;
  554. bool popupMenuEnabled = true;
  555. bool selectAllTextWhenFocused = false;
  556. bool scrollbarVisible = true;
  557. bool wasFocused = false;
  558. bool keepCaretOnScreen = true;
  559. bool tabKeyUsed = false;
  560. bool menuActive = false;
  561. bool valueTextNeedsUpdating = false;
  562. bool consumeEscAndReturnKeys = true;
  563. UndoManager undoManager;
  564. std::unique_ptr<CaretComponent> caret;
  565. Range<int> selection;
  566. int leftIndent = 4, topIndent = 4;
  567. unsigned int lastTransactionTime = 0;
  568. Font currentFont { 14.0f };
  569. mutable int totalNumChars = 0;
  570. int caretPosition = 0;
  571. OwnedArray<UniformTextSection> sections;
  572. String textToShowWhenEmpty;
  573. Colour colourForTextWhenEmpty;
  574. juce_wchar passwordCharacter;
  575. OptionalScopedPointer<InputFilter> inputFilter;
  576. Value textValue;
  577. VirtualKeyboardType keyboardType = TextInputTarget::textKeyboard;
  578. float lineSpacing = 1.0f;
  579. enum DragType
  580. {
  581. notDragging,
  582. draggingSelectionStart,
  583. draggingSelectionEnd
  584. };
  585. DragType dragType = notDragging;
  586. ListenerList<Listener> listeners;
  587. Array<Range<int>> underlinedSections;
  588. void moveCaret (int newCaretPos);
  589. void moveCaretTo (int newPosition, bool isSelecting);
  590. void recreateCaret();
  591. void handleCommandMessage (int) override;
  592. void coalesceSimilarSections();
  593. void splitSection (int sectionIndex, int charToSplitAt);
  594. void clearInternal (UndoManager*);
  595. void insert (const String&, int insertIndex, const Font&, Colour, UndoManager*, int newCaretPos);
  596. void reinsert (int insertIndex, const OwnedArray<UniformTextSection>&);
  597. void remove (Range<int>, UndoManager*, int caretPositionToMoveTo);
  598. void getCharPosition (int index, Point<float>&, float& lineHeight) const;
  599. Rectangle<float> getCaretRectangleFloat() const;
  600. void updateCaretPosition();
  601. void updateValueFromText();
  602. void textWasChangedByValue();
  603. int indexAtPosition (float x, float y);
  604. int findWordBreakAfter (int position) const;
  605. int findWordBreakBefore (int position) const;
  606. bool moveCaretWithTransaction (int newPos, bool selecting);
  607. void drawContent (Graphics&);
  608. void updateTextHolderSize();
  609. float getWordWrapWidth() const;
  610. float getJustificationWidth() const;
  611. void timerCallbackInt();
  612. void checkFocus();
  613. void repaintText (Range<int>);
  614. void scrollByLines (int deltaLines);
  615. bool undoOrRedo (bool shouldUndo);
  616. UndoManager* getUndoManager() noexcept;
  617. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextEditor)
  618. };
  619. } // namespace juce