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.

813 lines
33KB

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