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.

juce_TextEditor.h 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. An editable text box.
  24. A TextEditor can either be in single- or multi-line mode, and supports mixed
  25. fonts and colours.
  26. @see TextEditor::Listener, Label
  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();
  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. //==============================================================================
  196. /** If set to true, focusing on the editor will highlight all its text.
  197. (Set to false by default).
  198. This is useful for boxes where you expect the user to re-enter all the
  199. text when they focus on the component, rather than editing what's already there.
  200. */
  201. void setSelectAllWhenFocused (bool shouldSelectAll);
  202. /** When the text editor is empty, it can be set to display a message.
  203. This is handy for things like telling the user what to type in the box - the
  204. string is only displayed, it's not taken to actually be the contents of
  205. the editor.
  206. */
  207. void setTextToShowWhenEmpty (const String& text, Colour colourToUse);
  208. //==============================================================================
  209. /** Changes the size of the scrollbars that are used.
  210. Handy if you need smaller scrollbars for a small text box.
  211. */
  212. void setScrollBarThickness (int newThicknessPixels);
  213. //==============================================================================
  214. /**
  215. Receives callbacks from a TextEditor component when it changes.
  216. @see TextEditor::addListener
  217. */
  218. class JUCE_API Listener
  219. {
  220. public:
  221. /** Destructor. */
  222. virtual ~Listener() {}
  223. /** Called when the user changes the text in some way. */
  224. virtual void textEditorTextChanged (TextEditor&) {}
  225. /** Called when the user presses the return key. */
  226. virtual void textEditorReturnKeyPressed (TextEditor&) {}
  227. /** Called when the user presses the escape key. */
  228. virtual void textEditorEscapeKeyPressed (TextEditor&) {}
  229. /** Called when the text editor loses focus. */
  230. virtual void textEditorFocusLost (TextEditor&) {}
  231. };
  232. /** Registers a listener to be told when things happen to the text.
  233. @see removeListener
  234. */
  235. void addListener (Listener* newListener);
  236. /** Deregisters a listener.
  237. @see addListener
  238. */
  239. void removeListener (Listener* listenerToRemove);
  240. //==============================================================================
  241. /** Returns the entire contents of the editor. */
  242. String getText() const;
  243. /** Returns a section of the contents of the editor. */
  244. String getTextInRange (const Range<int>& textRange) const override;
  245. /** Returns true if there are no characters in the editor.
  246. This is far more efficient than calling getText().isEmpty().
  247. */
  248. bool isEmpty() const;
  249. /** Sets the entire content of the editor.
  250. This will clear the editor and insert the given text (using the current text colour
  251. and font). You can set the current text colour using
  252. @code setColour (TextEditor::textColourId, ...);
  253. @endcode
  254. @param newText the text to add
  255. @param sendTextChangeMessage if true, this will cause a change message to
  256. be sent to all the listeners.
  257. @see insertTextAtCaret
  258. */
  259. void setText (const String& newText,
  260. bool sendTextChangeMessage = true);
  261. /** Returns a Value object that can be used to get or set the text.
  262. Bear in mind that this operate quite slowly if your text box contains large
  263. amounts of text, as it needs to dynamically build the string that's involved.
  264. It's best used for small text boxes.
  265. */
  266. Value& getTextValue();
  267. /** Inserts some text at the current caret position.
  268. If a section of the text is highlighted, it will be replaced by
  269. this string, otherwise it will be inserted.
  270. To delete a section of text, you can use setHighlightedRegion() to
  271. highlight it, and call insertTextAtCaret (String()).
  272. @see setCaretPosition, getCaretPosition, setHighlightedRegion
  273. */
  274. void insertTextAtCaret (const String& textToInsert) override;
  275. /** Deletes all the text from the editor. */
  276. void clear();
  277. /** Deletes the currently selected region.
  278. This doesn't copy the deleted section to the clipboard - if you need to do that, call copy() first.
  279. @see copy, paste, SystemClipboard
  280. */
  281. void cut();
  282. /** Copies the currently selected region to the clipboard.
  283. @see cut, paste, SystemClipboard
  284. */
  285. void copy();
  286. /** Pastes the contents of the clipboard into the editor at the caret position.
  287. @see cut, copy, SystemClipboard
  288. */
  289. void paste();
  290. //==============================================================================
  291. /** Returns the current index of the caret.
  292. @see setCaretPosition
  293. */
  294. int getCaretPosition() const;
  295. /** Moves the caret to be in front of a given character.
  296. @see getCaretPosition, moveCaretToEnd
  297. */
  298. void setCaretPosition (int newIndex);
  299. /** Attempts to scroll the text editor so that the caret ends up at
  300. a specified position.
  301. This won't affect the caret's position within the text, it tries to scroll
  302. the entire editor vertically and horizontally so that the caret is sitting
  303. at the given position (relative to the top-left of this component).
  304. Depending on the amount of text available, it might not be possible to
  305. scroll far enough for the caret to reach this exact position, but it
  306. will go as far as it can in that direction.
  307. */
  308. void scrollEditorToPositionCaret (int desiredCaretX, int desiredCaretY);
  309. /** Get the graphical position of the caret.
  310. The rectangle returned is relative to the component's top-left corner.
  311. @see scrollEditorToPositionCaret
  312. */
  313. Rectangle<int> getCaretRectangle() override;
  314. /** Selects a section of the text. */
  315. void setHighlightedRegion (const Range<int>& newSelection) override;
  316. /** Returns the range of characters that are selected.
  317. If nothing is selected, this will return an empty range.
  318. @see setHighlightedRegion
  319. */
  320. Range<int> getHighlightedRegion() const override { return selection; }
  321. /** Returns the section of text that is currently selected. */
  322. String getHighlightedText() const;
  323. /** Finds the index of the character at a given position.
  324. The coordinates are relative to the component's top-left.
  325. */
  326. int getTextIndexAt (int x, int y);
  327. /** Counts the number of characters in the text.
  328. This is quicker than getting the text as a string if you just need to know
  329. the length.
  330. */
  331. int getTotalNumChars() const;
  332. /** Returns the total width of the text, as it is currently laid-out.
  333. This may be larger than the size of the TextEditor, and can change when
  334. the TextEditor is resized or the text changes.
  335. */
  336. int getTextWidth() const;
  337. /** Returns the maximum height of the text, as it is currently laid-out.
  338. This may be larger than the size of the TextEditor, and can change when
  339. the TextEditor is resized or the text changes.
  340. */
  341. int getTextHeight() const;
  342. /** Changes the size of the gap at the top and left-edge of the editor.
  343. By default there's a gap of 4 pixels.
  344. */
  345. void setIndents (int newLeftIndent, int newTopIndent);
  346. /** Changes the size of border left around the edge of the component.
  347. @see getBorder
  348. */
  349. void setBorder (const BorderSize<int>& border);
  350. /** Returns the size of border around the edge of the component.
  351. @see setBorder
  352. */
  353. BorderSize<int> getBorder() const;
  354. /** Used to disable the auto-scrolling which keeps the caret visible.
  355. If true (the default), the editor will scroll when the caret moves offscreen. If
  356. set to false, it won't.
  357. */
  358. void setScrollToShowCursor (bool shouldScrollToShowCaret);
  359. /** Modifies the horizontal justification of the text within the editor window. */
  360. void setJustification (Justification newJustification);
  361. /** Sets the line spacing of the TextEditor.
  362. The default (and minimum) value is 1.0 and values > 1.0 will increase the line spacing as a
  363. multiple of the line height e.g. for double-spacing call this method with an argument of 2.0.
  364. */
  365. void setLineSpacing (float newLineSpacing) noexcept { lineSpacing = jmax (1.0f, newLineSpacing); }
  366. /** Returns the current line spacing of the TextEditor. */
  367. float getLineSpacing() const noexcept { return lineSpacing; }
  368. //==============================================================================
  369. void moveCaretToEnd();
  370. bool moveCaretLeft (bool moveInWholeWordSteps, bool selecting);
  371. bool moveCaretRight (bool moveInWholeWordSteps, bool selecting);
  372. bool moveCaretUp (bool selecting);
  373. bool moveCaretDown (bool selecting);
  374. bool pageUp (bool selecting);
  375. bool pageDown (bool selecting);
  376. bool scrollDown();
  377. bool scrollUp();
  378. bool moveCaretToTop (bool selecting);
  379. bool moveCaretToStartOfLine (bool selecting);
  380. bool moveCaretToEnd (bool selecting);
  381. bool moveCaretToEndOfLine (bool selecting);
  382. bool deleteBackwards (bool moveInWholeWordSteps);
  383. bool deleteForwards (bool moveInWholeWordSteps);
  384. bool copyToClipboard();
  385. bool cutToClipboard();
  386. bool pasteFromClipboard();
  387. bool selectAll();
  388. bool undo();
  389. bool redo();
  390. //==============================================================================
  391. /** This adds the items to the popup menu.
  392. By default it adds the cut/copy/paste items, but you can override this if
  393. you need to replace these with your own items.
  394. If you want to add your own items to the existing ones, you can override this,
  395. call the base class's addPopupMenuItems() method, then append your own items.
  396. When the menu has been shown, performPopupMenuAction() will be called to
  397. perform the item that the user has chosen.
  398. The default menu items will be added using item IDs from the
  399. StandardApplicationCommandIDs namespace.
  400. If this was triggered by a mouse-click, the mouseClickEvent parameter will be
  401. a pointer to the info about it, or may be null if the menu is being triggered
  402. by some other means.
  403. @see performPopupMenuAction, setPopupMenuEnabled, isPopupMenuEnabled
  404. */
  405. virtual void addPopupMenuItems (PopupMenu& menuToAddTo,
  406. const MouseEvent* mouseClickEvent);
  407. /** This is called to perform one of the items that was shown on the popup menu.
  408. If you've overridden addPopupMenuItems(), you should also override this
  409. to perform the actions that you've added.
  410. If you've overridden addPopupMenuItems() but have still left the default items
  411. on the menu, remember to call the superclass's performPopupMenuAction()
  412. so that it can perform the default actions if that's what the user clicked on.
  413. @see addPopupMenuItems, setPopupMenuEnabled, isPopupMenuEnabled
  414. */
  415. virtual void performPopupMenuAction (int menuItemID);
  416. //==============================================================================
  417. /** Base class for input filters that can be applied to a TextEditor to restrict
  418. the text that can be entered.
  419. */
  420. class JUCE_API InputFilter
  421. {
  422. public:
  423. InputFilter() {}
  424. virtual ~InputFilter() {}
  425. /** This method is called whenever text is entered into the editor.
  426. An implementation of this class should should check the input string,
  427. and return an edited version of it that should be used.
  428. */
  429. virtual String filterNewText (TextEditor&, const String& newInput) = 0;
  430. };
  431. /** An input filter for a TextEditor that limits the length of text and/or the
  432. characters that it may contain.
  433. */
  434. class JUCE_API LengthAndCharacterRestriction : public InputFilter
  435. {
  436. public:
  437. /** Creates a filter that limits the length of text, and/or the characters that it can contain.
  438. @param maxNumChars if this is > 0, it sets a maximum length limit; if <= 0, no
  439. limit is set
  440. @param allowedCharacters if this is non-empty, then only characters that occur in
  441. this string are allowed to be entered into the editor.
  442. */
  443. LengthAndCharacterRestriction (int maxNumChars, const String& allowedCharacters);
  444. String filterNewText (TextEditor&, const String&) override;
  445. private:
  446. String allowedCharacters;
  447. int maxLength;
  448. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LengthAndCharacterRestriction)
  449. };
  450. /** Sets an input filter that should be applied to this editor.
  451. The filter can be nullptr, to remove any existing filters.
  452. If takeOwnership is true, then the filter will be owned and deleted by the editor
  453. when no longer needed.
  454. */
  455. void setInputFilter (InputFilter* newFilter, bool takeOwnership);
  456. /** Returns the current InputFilter, as set by setInputFilter(). */
  457. InputFilter* getInputFilter() const noexcept { return inputFilter; }
  458. /** Sets limits on the characters that can be entered.
  459. This is just a shortcut that passes an instance of the LengthAndCharacterRestriction
  460. class to setInputFilter().
  461. @param maxTextLength 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. void setInputRestrictions (int maxTextLength,
  467. const String& allowedCharacters = String());
  468. void setKeyboardType (VirtualKeyboardType type) noexcept { keyboardType = type; }
  469. //==============================================================================
  470. /** This abstract base class is implemented by LookAndFeel classes to provide
  471. TextEditor drawing functionality.
  472. */
  473. struct JUCE_API LookAndFeelMethods
  474. {
  475. virtual ~LookAndFeelMethods() {}
  476. virtual void fillTextEditorBackground (Graphics&, int width, int height, TextEditor&) = 0;
  477. virtual void drawTextEditorOutline (Graphics&, int width, int height, TextEditor&) = 0;
  478. virtual CaretComponent* createCaretComponent (Component* keyFocusOwner) = 0;
  479. };
  480. //==============================================================================
  481. /** @internal */
  482. void paint (Graphics&) override;
  483. /** @internal */
  484. void paintOverChildren (Graphics&) override;
  485. /** @internal */
  486. void mouseDown (const MouseEvent&) override;
  487. /** @internal */
  488. void mouseUp (const MouseEvent&) override;
  489. /** @internal */
  490. void mouseDrag (const MouseEvent&) override;
  491. /** @internal */
  492. void mouseDoubleClick (const MouseEvent&) override;
  493. /** @internal */
  494. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  495. /** @internal */
  496. bool keyPressed (const KeyPress&) override;
  497. /** @internal */
  498. bool keyStateChanged (bool) override;
  499. /** @internal */
  500. void focusGained (FocusChangeType) override;
  501. /** @internal */
  502. void focusLost (FocusChangeType) override;
  503. /** @internal */
  504. void resized() override;
  505. /** @internal */
  506. void enablementChanged() override;
  507. /** @internal */
  508. void lookAndFeelChanged() override;
  509. /** @internal */
  510. void parentHierarchyChanged() override;
  511. /** @internal */
  512. bool isTextInputActive() const override;
  513. /** @internal */
  514. void setTemporaryUnderlining (const Array<Range<int> >&) override;
  515. /** @internal */
  516. VirtualKeyboardType getKeyboardType() override { return keyboardType; }
  517. protected:
  518. //==============================================================================
  519. /** Scrolls the minimum distance needed to get the caret into view. */
  520. void scrollToMakeSureCursorIsVisible();
  521. /** Used internally to dispatch a text-change message. */
  522. void textChanged();
  523. /** Begins a new transaction in the UndoManager. */
  524. void newTransaction();
  525. /** Can be overridden to intercept return key presses directly */
  526. virtual void returnPressed();
  527. /** Can be overridden to intercept escape key presses directly */
  528. virtual void escapePressed();
  529. private:
  530. //==============================================================================
  531. JUCE_PUBLIC_IN_DLL_BUILD (class UniformTextSection)
  532. struct Iterator;
  533. struct TextHolderComponent;
  534. struct TextEditorViewport;
  535. struct InsertAction;
  536. struct RemoveAction;
  537. ScopedPointer<Viewport> viewport;
  538. TextHolderComponent* textHolder;
  539. BorderSize<int> borderSize { 1, 1, 1, 3 };
  540. Justification justification { Justification::left };
  541. bool readOnly = false;
  542. bool caretVisible = true;
  543. bool multiline = false;
  544. bool wordWrap = false;
  545. bool returnKeyStartsNewLine = false;
  546. bool popupMenuEnabled = true;
  547. bool selectAllTextWhenFocused = false;
  548. bool scrollbarVisible = true;
  549. bool wasFocused = false;
  550. bool keepCaretOnScreen = true;
  551. bool tabKeyUsed = false;
  552. bool menuActive = false;
  553. bool valueTextNeedsUpdating = false;
  554. bool consumeEscAndReturnKeys = true;
  555. UndoManager undoManager;
  556. ScopedPointer<CaretComponent> caret;
  557. Range<int> selection;
  558. int leftIndent = 4, topIndent = 4;
  559. unsigned int lastTransactionTime = 0;
  560. Font currentFont { 14.0f };
  561. mutable int totalNumChars = 0;
  562. int caretPosition = 0;
  563. OwnedArray<UniformTextSection> sections;
  564. String textToShowWhenEmpty;
  565. Colour colourForTextWhenEmpty;
  566. juce_wchar passwordCharacter;
  567. OptionalScopedPointer<InputFilter> inputFilter;
  568. Value textValue;
  569. VirtualKeyboardType keyboardType = TextInputTarget::textKeyboard;
  570. float lineSpacing = 1.0f;
  571. enum DragType
  572. {
  573. notDragging,
  574. draggingSelectionStart,
  575. draggingSelectionEnd
  576. };
  577. DragType dragType = notDragging;
  578. ListenerList<Listener> listeners;
  579. Array<Range<int>> underlinedSections;
  580. void moveCaret (int newCaretPos);
  581. void moveCaretTo (int newPosition, bool isSelecting);
  582. void recreateCaret();
  583. void handleCommandMessage (int) override;
  584. void coalesceSimilarSections();
  585. void splitSection (int sectionIndex, int charToSplitAt);
  586. void clearInternal (UndoManager*);
  587. void insert (const String&, int insertIndex, const Font&, Colour, UndoManager*, int newCaretPos);
  588. void reinsert (int insertIndex, const OwnedArray<UniformTextSection>&);
  589. void remove (Range<int>, UndoManager*, int caretPositionToMoveTo);
  590. void getCharPosition (int index, Point<float>&, float& lineHeight) const;
  591. void updateCaretPosition();
  592. void updateValueFromText();
  593. void textWasChangedByValue();
  594. int indexAtPosition (float x, float y);
  595. int findWordBreakAfter (int position) const;
  596. int findWordBreakBefore (int position) const;
  597. bool moveCaretWithTransaction (int newPos, bool selecting);
  598. void drawContent (Graphics&);
  599. void updateTextHolderSize();
  600. float getWordWrapWidth() const;
  601. float getJustificationWidth() const;
  602. void timerCallbackInt();
  603. void repaintText (Range<int>);
  604. void scrollByLines (int deltaLines);
  605. bool undoOrRedo (bool shouldUndo);
  606. UndoManager* getUndoManager() noexcept;
  607. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextEditor)
  608. };
  609. /** This typedef is just for compatibility with old code - newer code should use the TextEditor::Listener class directly. */
  610. typedef TextEditor::Listener TextEditorListener;
  611. } // namespace juce