JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_TextEditor.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29//==============================================================================
40class JUCE_API TextEditor : public TextInputTarget,
41 public Component,
43{
44public:
45 //==============================================================================
56 explicit TextEditor (const String& componentName = String(),
57 juce_wchar passwordCharacter = 0);
58
60 ~TextEditor() override;
61
62 //==============================================================================
78 void setMultiLine (bool shouldBeMultiLine,
79 bool shouldWordWrap = true);
80
82 bool isMultiLine() const;
83
84 //==============================================================================
92 void setReturnKeyStartsNewLine (bool shouldStartNewLine);
93
97 bool getReturnKeyStartsNewLine() const { return returnKeyStartsNewLine; }
98
105 void setTabKeyUsedAsCharacter (bool shouldTabKeyBeUsed);
106
110 bool isTabKeyUsedAsCharacter() const { return tabKeyUsed; }
111
118 void setEscapeAndReturnKeysConsumed (bool shouldBeConsumed) noexcept;
119
120 //==============================================================================
130 void setReadOnly (bool shouldBeReadOnly);
131
133 bool isReadOnly() const noexcept;
134
135 //==============================================================================
140 void setCaretVisible (bool shouldBeVisible);
141
145 bool isCaretVisible() const noexcept { return caretVisible && ! isReadOnly(); }
146
147 //==============================================================================
156 void setScrollbarsShown (bool shouldBeEnabled);
157
161 bool areScrollbarsShown() const noexcept { return scrollbarVisible; }
162
172 void setPasswordCharacter (juce_wchar passwordCharacter);
173
177 juce_wchar getPasswordCharacter() const noexcept { return passwordCharacter; }
178
179 //==============================================================================
187 void setPopupMenuEnabled (bool menuEnabled);
188
192 bool isPopupMenuEnabled() const noexcept { return popupMenuEnabled; }
193
195 bool isPopupMenuCurrentlyActive() const noexcept { return menuActive; }
196
197 //==============================================================================
208 {
209 backgroundColourId = 0x1000200,
212 textColourId = 0x1000201,
217 highlightColourId = 0x1000202,
221 highlightedTextColourId = 0x1000203,
223 outlineColourId = 0x1000205,
226 focusedOutlineColourId = 0x1000206,
229 shadowColourId = 0x1000207,
231 };
232
233 //==============================================================================
242 void setFont (const Font& newFont);
243
251 void applyFontToAllText (const Font& newFont, bool changeCurrentFont = true);
252
257 const Font& getFont() const noexcept { return currentFont; }
258
264 void applyColourToAllText (const Colour& newColour, bool changeCurrentTextColour = true);
265
270 void setWhitespaceUnderlined (bool shouldUnderlineWhitespace) noexcept { underlineWhitespace = shouldUnderlineWhitespace; }
271
276 bool isWhitespaceUnderlined() const noexcept { return underlineWhitespace; }
277
278 //==============================================================================
286 void setSelectAllWhenFocused (bool shouldSelectAll);
287
294 void setTextToShowWhenEmpty (const String& text, Colour colourToUse);
295
300 String getTextToShowWhenEmpty() const noexcept { return textToShowWhenEmpty; }
301
302 //==============================================================================
306 void setScrollBarThickness (int newThicknessPixels);
307
308 //==============================================================================
314 class JUCE_API Listener
315 {
316 public:
318 virtual ~Listener() = default;
319
322
325
328
331 };
332
336 void addListener (Listener* newListener);
337
341 void removeListener (Listener* listenerToRemove);
342
343 //==============================================================================
346
349
352
355
356 //==============================================================================
358 String getText() const;
359
361 String getTextInRange (const Range<int>& textRange) const override;
362
366 bool isEmpty() const;
367
380 void setText (const String& newText,
381 bool sendTextChangeMessage = true);
382
389 Value& getTextValue();
390
401 void insertTextAtCaret (const String& textToInsert) override;
402
404 void clear();
405
410 void cut();
411
415 void copy();
416
420 void paste();
421
422 //==============================================================================
426 int getCaretPosition() const override;
427
431 void setCaretPosition (int newIndex);
432
444 void scrollEditorToPositionCaret (int desiredCaretX, int desiredCaretY);
445
450 Rectangle<int> getCaretRectangleForCharIndex (int index) const override;
451
453 void setHighlightedRegion (const Range<int>& newSelection) override;
454
459 Range<int> getHighlightedRegion() const override { return selection; }
460
462 String getHighlightedText() const;
463
467 int getTextIndexAt (int x, int y) const;
468
472 int getTextIndexAt (Point<int>) const;
473
477 int getCharIndexForPoint (Point<int> point) const override;
478
484 int getTotalNumChars() const override;
485
491 int getTextWidth() const;
492
498 int getTextHeight() const;
499
503 void setIndents (int newLeftIndent, int newTopIndent);
504
508 int getTopIndent() const noexcept { return topIndent; }
509
513 int getLeftIndent() const noexcept { return leftIndent; }
514
518 void setBorder (BorderSize<int> border);
519
523 BorderSize<int> getBorder() const;
524
530 void setScrollToShowCursor (bool shouldScrollToShowCaret);
531
533 void setJustification (Justification newJustification);
534
536 Justification getJustificationType() const noexcept { return justification; }
537
542 void setLineSpacing (float newLineSpacing) noexcept { lineSpacing = jmax (1.0f, newLineSpacing); }
543
545 float getLineSpacing() const noexcept { return lineSpacing; }
546
553 RectangleList<int> getTextBounds (Range<int> textRange) const override;
554
555 //==============================================================================
556 void moveCaretToEnd();
557 bool moveCaretLeft (bool moveInWholeWordSteps, bool selecting);
558 bool moveCaretRight (bool moveInWholeWordSteps, bool selecting);
559 bool moveCaretUp (bool selecting);
560 bool moveCaretDown (bool selecting);
561 bool pageUp (bool selecting);
562 bool pageDown (bool selecting);
563 bool scrollDown();
564 bool scrollUp();
565 bool moveCaretToTop (bool selecting);
566 bool moveCaretToStartOfLine (bool selecting);
567 bool moveCaretToEnd (bool selecting);
568 bool moveCaretToEndOfLine (bool selecting);
569 bool deleteBackwards (bool moveInWholeWordSteps);
570 bool deleteForwards (bool moveInWholeWordSteps);
571 bool copyToClipboard();
572 bool cutToClipboard();
573 bool pasteFromClipboard();
574 bool selectAll();
575 bool undo();
576 bool redo();
577
578 //==============================================================================
599 virtual void addPopupMenuItems (PopupMenu& menuToAddTo,
600 const MouseEvent* mouseClickEvent);
601
613 virtual void performPopupMenuAction (int menuItemID);
614
615 //==============================================================================
619 class JUCE_API InputFilter
620 {
621 public:
622 InputFilter() = default;
623 virtual ~InputFilter() = default;
624
629 virtual String filterNewText (TextEditor&, const String& newInput) = 0;
630 };
631
636 {
637 public:
644 LengthAndCharacterRestriction (int maxNumChars, const String& allowedCharacters);
645
646 String filterNewText (TextEditor&, const String&) override;
647
648 private:
649 String allowedCharacters;
650 int maxLength;
651
653 };
654
660 void setInputFilter (InputFilter* newFilter, bool takeOwnership);
661
663 InputFilter* getInputFilter() const noexcept { return inputFilter; }
664
674 void setInputRestrictions (int maxTextLength,
675 const String& allowedCharacters = String());
676
680 void setKeyboardType (VirtualKeyboardType type) noexcept { keyboardType = type; }
681
688 void setClicksOutsideDismissVirtualKeyboard (bool);
689
693 bool getClicksOutsideDismissVirtualKeyboard() const { return clicksOutsideDismissVirtualKeyboard; }
694
695 //==============================================================================
699 struct JUCE_API LookAndFeelMethods
700 {
701 virtual ~LookAndFeelMethods() = default;
702
703 virtual void fillTextEditorBackground (Graphics&, int width, int height, TextEditor&) = 0;
704 virtual void drawTextEditorOutline (Graphics&, int width, int height, TextEditor&) = 0;
705
706 virtual CaretComponent* createCaretComponent (Component* keyFocusOwner) = 0;
707 };
708
709 //==============================================================================
711 void paint (Graphics&) override;
713 void paintOverChildren (Graphics&) override;
715 void mouseDown (const MouseEvent&) override;
717 void mouseUp (const MouseEvent&) override;
719 void mouseDrag (const MouseEvent&) override;
721 void mouseDoubleClick (const MouseEvent&) override;
723 void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
725 bool keyPressed (const KeyPress&) override;
727 bool keyStateChanged (bool) override;
729 void focusGained (FocusChangeType) override;
731 void focusLost (FocusChangeType) override;
733 void resized() override;
735 void enablementChanged() override;
737 void lookAndFeelChanged() override;
739 void parentHierarchyChanged() override;
741 bool isTextInputActive() const override;
743 void setTemporaryUnderlining (const Array<Range<int>>&) override;
745 VirtualKeyboardType getKeyboardType() override;
747 std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
748
749protected:
750 //==============================================================================
752 void scrollToMakeSureCursorIsVisible();
753
755 void textChanged();
756
758 void newTransaction();
759
761 virtual void returnPressed();
762
764 virtual void escapePressed();
765
766private:
767 //==============================================================================
768 JUCE_PUBLIC_IN_DLL_BUILD (class UniformTextSection)
769 struct Iterator;
770 struct TextHolderComponent;
771 struct TextEditorViewport;
772 struct InsertAction;
773 struct RemoveAction;
775
776 class GlobalMouseListener : private MouseListener
777 {
778 public:
779 explicit GlobalMouseListener (Component& e) : editor (e) { Desktop::getInstance().addGlobalMouseListener (this); }
780 ~GlobalMouseListener() override { Desktop::getInstance().removeGlobalMouseListener (this); }
781
782 bool lastMouseDownInEditor() const { return mouseDownInEditor; }
783
784 private:
785 void mouseDown (const MouseEvent& event) override { mouseDownInEditor = event.originalComponent == &editor; }
786
787 Component& editor;
788 bool mouseDownInEditor = false;
789 };
790
792 TextHolderComponent* textHolder;
793 BorderSize<int> borderSize { 1, 1, 1, 3 };
794 Justification justification { Justification::topLeft };
795 const GlobalMouseListener globalMouseListener { *this };
796
797 bool readOnly = false;
798 bool caretVisible = true;
799 bool multiline = false;
800 bool wordWrap = false;
801 bool returnKeyStartsNewLine = false;
802 bool popupMenuEnabled = true;
803 bool selectAllTextWhenFocused = false;
804 bool scrollbarVisible = true;
805 bool wasFocused = false;
806 bool keepCaretOnScreen = true;
807 bool tabKeyUsed = false;
808 bool menuActive = false;
809 bool valueTextNeedsUpdating = false;
810 bool consumeEscAndReturnKeys = true;
811 bool underlineWhitespace = true;
812 bool clicksOutsideDismissVirtualKeyboard = false;
813
814 UndoManager undoManager;
816 Range<int> selection;
817 int leftIndent = 4, topIndent = 4;
818 unsigned int lastTransactionTime = 0;
819 Font currentFont { 14.0f };
820 mutable int totalNumChars = 0;
821 int caretPosition = 0;
822 OwnedArray<UniformTextSection> sections;
823 String textToShowWhenEmpty;
824 Colour colourForTextWhenEmpty;
825 juce_wchar passwordCharacter;
826 OptionalScopedPointer<InputFilter> inputFilter;
827 Value textValue;
828 VirtualKeyboardType keyboardType = TextInputTarget::textKeyboard;
829 float lineSpacing = 1.0f;
830
831 enum DragType
832 {
833 notDragging,
834 draggingSelectionStart,
835 draggingSelectionEnd
836 };
837
838 DragType dragType = notDragging;
839
840 ListenerList<Listener> listeners;
841 Array<Range<int>> underlinedSections;
842
843 void moveCaret (int newCaretPos);
844 void moveCaretTo (int newPosition, bool isSelecting);
845 void recreateCaret();
846 void handleCommandMessage (int) override;
847 void coalesceSimilarSections();
848 void splitSection (int sectionIndex, int charToSplitAt);
849 void clearInternal (UndoManager*);
850 void insert (const String&, int insertIndex, const Font&, Colour, UndoManager*, int newCaretPos);
851 void reinsert (int insertIndex, const OwnedArray<UniformTextSection>&);
852 void remove (Range<int>, UndoManager*, int caretPositionToMoveTo);
853 void getCharPosition (int index, Point<float>&, float& lineHeight) const;
854 void updateCaretPosition();
855 void updateValueFromText();
856 void textWasChangedByValue();
857 int indexAtPosition (float x, float y) const;
858 int findWordBreakAfter (int position) const;
859 int findWordBreakBefore (int position) const;
860 bool moveCaretWithTransaction (int newPos, bool selecting);
861 void drawContent (Graphics&);
862 void checkLayout();
863 int getWordWrapWidth() const;
864 int getMaximumTextWidth() const;
865 int getMaximumTextHeight() const;
866 void timerCallbackInt();
867 void checkFocus();
868 void repaintText (Range<int>);
869 void scrollByLines (int deltaLines);
870 bool undoOrRedo (bool shouldUndo);
871 UndoManager* getUndoManager() noexcept;
872 void setSelection (Range<int>) noexcept;
873 Point<int> getTextOffset() const noexcept;
874
876};
877
878
879} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
Specifies a set of gaps to be left around the sides of a rectangle.
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
The base class for all JUCE user-interface objects.
FocusChangeType
Enumeration used by the focusGained() and focusLost() methods.
Represents a particular font, including its size, style, etc.
Definition juce_Font.h:42
A graphics context, used for drawing a component or image.
Represents a type of justification to be used when positioning graphical items.
Represents a key press, including any modifier keys that are needed.
Contains position and status information about a mouse event.
A MouseListener can be registered with a component to receive callbacks about mouse events that happe...
A pair of (x, y) coordinates.
Definition juce_Point.h:42
Creates and displays a popup-menu.
A general-purpose range object, that simply represents any linear range with a start and end point.
Definition juce_Range.h:40
Maintains a set of rectangles as a complex region.
Manages a rectangle and allows geometric operations to be performed on it.
An implementation of TooltipClient that stores the tooltip string and a method for changing it.
The JUCE String class!
Definition juce_String.h:53
Base class for input filters that can be applied to a TextEditor to restrict the text that can be ent...
virtual String filterNewText(TextEditor &, const String &newInput)=0
This method is called whenever text is entered into the editor.
An input filter for a TextEditor that limits the length of text and/or the characters that it may con...
Receives callbacks from a TextEditor component when it changes.
virtual void textEditorTextChanged(TextEditor &)
Called when the user changes the text in some way.
virtual ~Listener()=default
Destructor.
virtual void textEditorFocusLost(TextEditor &)
Called when the text editor loses focus.
virtual void textEditorReturnKeyPressed(TextEditor &)
Called when the user presses the return key.
virtual void textEditorEscapeKeyPressed(TextEditor &)
Called when the user presses the escape key.
An editable text box.
bool isWhitespaceUnderlined() const noexcept
Returns true if whitespace is underlined for underlined fonts.
bool getClicksOutsideDismissVirtualKeyboard() const
Returns true if the editor is configured to hide the virtual keyboard when the mouse is pressed on an...
ColourIds
A set of colour IDs to use to change the colour of various aspects of the editor.
InputFilter * getInputFilter() const noexcept
Returns the current InputFilter, as set by setInputFilter().
bool isTabKeyUsedAsCharacter() const
Returns true if the tab key is being used for input.
bool isPopupMenuEnabled() const noexcept
Returns true if the right-click menu is enabled.
void setWhitespaceUnderlined(bool shouldUnderlineWhitespace) noexcept
Sets whether whitespace should be underlined when the editor font is underlined.
bool getReturnKeyStartsNewLine() const
Returns the value set by setReturnKeyStartsNewLine().
Justification getJustificationType() const noexcept
Returns the type of justification, as set in setJustification().
const Font & getFont() const noexcept
Returns the font that's currently being used for new text.
std::function< void()> onFocusLost
You can assign a lambda to this callback object to have it called when the editor loses key focus.
std::function< void()> onEscapeKey
You can assign a lambda to this callback object to have it called when the escape key is pressed.
bool isPopupMenuCurrentlyActive() const noexcept
Returns true if a popup-menu is currently being displayed.
void setLineSpacing(float newLineSpacing) noexcept
Sets the line spacing of the TextEditor.
std::function< void()> onReturnKey
You can assign a lambda to this callback object to have it called when the return key is pressed.
int getLeftIndent() const noexcept
Returns the gap at the left edge of the editor.
float getLineSpacing() const noexcept
Returns the current line spacing of the TextEditor.
int getTopIndent() const noexcept
Returns the gap at the top edge of the editor.
Range< int > getHighlightedRegion() const override
Returns the range of characters that are selected.
bool areScrollbarsShown() const noexcept
Returns true if scrollbars are enabled.
juce_wchar getPasswordCharacter() const noexcept
Returns the current password character.
String getTextToShowWhenEmpty() const noexcept
Returns the text that will be shown when the text editor is empty.
std::function< void()> onTextChange
You can assign a lambda to this callback object to have it called when the text is changed.
void setKeyboardType(VirtualKeyboardType type) noexcept
Sets the type of virtual keyboard that should be displayed when this editor has focus.
An abstract base class which can be implemented by components that function as text editors.
VirtualKeyboardType
A set of possible on-screen keyboard types, for use in the getKeyboardType() method.
Represents a shared variant value.
Definition juce_Value.h:51
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
JUCE Namespace.
wchar_t juce_wchar
A platform-independent 32-bit unicode character type.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
@ textChanged
Indicates that the visible text of a text element has changed.
Contains status information about a mouse wheel event.
remove
This abstract base class is implemented by LookAndFeel classes to provide TextEditor drawing function...