openshot-audio  0.1.2
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) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifndef JUCE_TEXTEDITOR_H_INCLUDED
26 #define JUCE_TEXTEDITOR_H_INCLUDED
27 
28 
29 //==============================================================================
38 class JUCE_API TextEditor : public Component,
39  public TextInputTarget,
41 {
42 public:
43  //==============================================================================
54  explicit TextEditor (const String& componentName = String::empty,
55  juce_wchar passwordCharacter = 0);
56 
58  ~TextEditor();
59 
60  //==============================================================================
71  void setMultiLine (bool shouldBeMultiLine,
72  bool shouldWordWrap = true);
73 
75  bool isMultiLine() const;
76 
77  //==============================================================================
85  void setReturnKeyStartsNewLine (bool shouldStartNewLine);
86 
90  bool getReturnKeyStartsNewLine() const { return returnKeyStartsNewLine; }
91 
98  void setTabKeyUsedAsCharacter (bool shouldTabKeyBeUsed);
99 
103  bool isTabKeyUsedAsCharacter() const { return tabKeyUsed; }
104 
111  void setEscapeAndReturnKeysConsumed (bool shouldBeConsumed) noexcept;
112 
113  //==============================================================================
123  void setReadOnly (bool shouldBeReadOnly);
124 
126  bool isReadOnly() const;
127 
128  //==============================================================================
133  void setCaretVisible (bool shouldBeVisible);
134 
138  bool isCaretVisible() const noexcept { return caret != nullptr; }
139 
140  //==============================================================================
149  void setScrollbarsShown (bool shouldBeEnabled);
150 
154  bool areScrollbarsShown() const noexcept { return scrollbarVisible; }
155 
156 
166  void setPasswordCharacter (juce_wchar passwordCharacter);
167 
171  juce_wchar getPasswordCharacter() const noexcept { return passwordCharacter; }
172 
173 
174  //==============================================================================
182  void setPopupMenuEnabled (bool menuEnabled);
183 
187  bool isPopupMenuEnabled() const noexcept { return popupMenuEnabled; }
188 
190  bool isPopupMenuCurrentlyActive() const noexcept { return menuActive; }
191 
192  //==============================================================================
203  {
204  backgroundColourId = 0x1000200,
207  textColourId = 0x1000201,
212  highlightColourId = 0x1000202,
216  highlightedTextColourId = 0x1000203,
218  outlineColourId = 0x1000205,
221  focusedOutlineColourId = 0x1000206,
224  shadowColourId = 0x1000207,
226  };
227 
228  //==============================================================================
237  void setFont (const Font& newFont);
238 
243  void applyFontToAllText (const Font& newFont);
244 
248  const Font& getFont() const noexcept { return currentFont; }
249 
250  //==============================================================================
258  void setSelectAllWhenFocused (bool shouldSelectAll);
259 
266  void setTextToShowWhenEmpty (const String& text, Colour colourToUse);
267 
268  //==============================================================================
272  void setScrollBarThickness (int newThicknessPixels);
273 
274  //==============================================================================
281  {
282  public:
284  virtual ~Listener() {}
285 
288 
291 
294 
296  virtual void textEditorFocusLost (TextEditor&) {}
297  };
298 
302  void addListener (Listener* newListener);
303 
307  void removeListener (Listener* listenerToRemove);
308 
309  //==============================================================================
311  String getText() const;
312 
314  String getTextInRange (const Range<int>& textRange) const override;
315 
319  bool isEmpty() const;
320 
333  void setText (const String& newText,
334  bool sendTextChangeMessage = true);
335 
342  Value& getTextValue();
343 
354  void insertTextAtCaret (const String& textToInsert) override;
355 
357  void clear();
358 
363  void cut();
364 
368  void copy();
369 
373  void paste();
374 
375  //==============================================================================
379  int getCaretPosition() const;
380 
384  void setCaretPosition (int newIndex);
385 
397  void scrollEditorToPositionCaret (int desiredCaretX, int desiredCaretY);
398 
404  Rectangle<int> getCaretRectangle() override;
405 
407  void setHighlightedRegion (const Range<int>& newSelection) override;
408 
413  Range<int> getHighlightedRegion() const override { return selection; }
414 
416  String getHighlightedText() const;
417 
421  int getTextIndexAt (int x, int y);
422 
428  int getTotalNumChars() const;
429 
435  int getTextWidth() const;
436 
442  int getTextHeight() const;
443 
447  void setIndents (int newLeftIndent, int newTopIndent);
448 
452  void setBorder (const BorderSize<int>& border);
453 
457  BorderSize<int> getBorder() const;
458 
464  void setScrollToShowCursor (bool shouldScrollToShowCaret);
465 
466  //==============================================================================
467  void moveCaretToEnd();
468  bool moveCaretLeft (bool moveInWholeWordSteps, bool selecting);
469  bool moveCaretRight (bool moveInWholeWordSteps, bool selecting);
470  bool moveCaretUp (bool selecting);
471  bool moveCaretDown (bool selecting);
472  bool pageUp (bool selecting);
473  bool pageDown (bool selecting);
474  bool scrollDown();
475  bool scrollUp();
476  bool moveCaretToTop (bool selecting);
477  bool moveCaretToStartOfLine (bool selecting);
478  bool moveCaretToEnd (bool selecting);
479  bool moveCaretToEndOfLine (bool selecting);
480  bool deleteBackwards (bool moveInWholeWordSteps);
481  bool deleteForwards (bool moveInWholeWordSteps);
482  bool copyToClipboard();
483  bool cutToClipboard();
484  bool pasteFromClipboard();
485  bool selectAll();
486  bool undo();
487  bool redo();
488 
489  //==============================================================================
510  virtual void addPopupMenuItems (PopupMenu& menuToAddTo,
511  const MouseEvent* mouseClickEvent);
512 
524  virtual void performPopupMenuAction (int menuItemID);
525 
526  //==============================================================================
531  {
532  public:
534  virtual ~InputFilter() {}
535 
540  virtual String filterNewText (TextEditor&, const String& newInput) = 0;
541  };
542 
547  {
548  public:
555  LengthAndCharacterRestriction (int maxNumChars, const String& allowedCharacters);
556 
557  private:
558  String allowedCharacters;
559  int maxLength;
560 
561  String filterNewText (TextEditor&, const String&) override;
562 
564  };
565 
571  void setInputFilter (InputFilter* newFilter, bool takeOwnership);
572 
574  InputFilter* getInputFilter() const noexcept { return inputFilter; }
575 
585  void setInputRestrictions (int maxTextLength,
586  const String& allowedCharacters = String::empty);
587 
588  void setKeyboardType (VirtualKeyboardType type) noexcept { keyboardType = type; }
589 
590  //==============================================================================
595  {
596  virtual ~LookAndFeelMethods() {}
597 
598  virtual void fillTextEditorBackground (Graphics&, int width, int height, TextEditor&) = 0;
599  virtual void drawTextEditorOutline (Graphics&, int width, int height, TextEditor&) = 0;
600 
601  virtual CaretComponent* createCaretComponent (Component* keyFocusOwner) = 0;
602  };
603 
604  //==============================================================================
606  void paint (Graphics&) override;
608  void paintOverChildren (Graphics&) override;
610  void mouseDown (const MouseEvent&) override;
612  void mouseUp (const MouseEvent&) override;
614  void mouseDrag (const MouseEvent&) override;
616  void mouseDoubleClick (const MouseEvent&) override;
618  void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
620  bool keyPressed (const KeyPress&) override;
622  bool keyStateChanged (bool) override;
624  void focusGained (FocusChangeType) override;
626  void focusLost (FocusChangeType) override;
628  void resized() override;
630  void enablementChanged() override;
632  void colourChanged() override;
634  void lookAndFeelChanged() override;
636  bool isTextInputActive() const override;
638  void setTemporaryUnderlining (const Array<Range<int> >&) override;
640  VirtualKeyboardType getKeyboardType() override { return keyboardType; }
641 
642 protected:
643  //==============================================================================
645  void scrollToMakeSureCursorIsVisible();
646 
648  void textChanged();
649 
651  void newTransaction();
652 
654  virtual void returnPressed();
655 
657  virtual void escapePressed();
658 
659 private:
660  //==============================================================================
661  class Iterator;
663  class TextHolderComponent;
664  class InsertAction;
666  friend class InsertAction;
667  friend class RemoveAction;
668 
669  ScopedPointer<Viewport> viewport;
670  TextHolderComponent* textHolder;
672 
673  bool readOnly : 1;
674  bool multiline : 1;
675  bool wordWrap : 1;
676  bool returnKeyStartsNewLine : 1;
677  bool popupMenuEnabled : 1;
678  bool selectAllTextWhenFocused : 1;
679  bool scrollbarVisible : 1;
680  bool wasFocused : 1;
681  bool keepCaretOnScreen : 1;
682  bool tabKeyUsed : 1;
683  bool menuActive : 1;
684  bool valueTextNeedsUpdating : 1;
685  bool consumeEscAndReturnKeys : 1;
686 
687  UndoManager undoManager;
689  Range<int> selection;
690  int leftIndent, topIndent;
691  unsigned int lastTransactionTime;
692  Font currentFont;
693  mutable int totalNumChars;
694  int caretPosition;
696  String textToShowWhenEmpty;
697  Colour colourForTextWhenEmpty;
698  juce_wchar passwordCharacter;
700  Value textValue;
701  VirtualKeyboardType keyboardType;
702 
703  enum
704  {
705  notDragging,
706  draggingSelectionStart,
707  draggingSelectionEnd
708  } dragType;
709 
710  ListenerList<Listener> listeners;
711  Array<Range<int> > underlinedSections;
712 
713  void moveCaret (int newCaretPos);
714  void moveCaretTo (int newPosition, bool isSelecting);
715  void handleCommandMessage (int) override;
716  void coalesceSimilarSections();
717  void splitSection (int sectionIndex, int charToSplitAt);
718  void clearInternal (UndoManager*);
719  void insert (const String&, int insertIndex, const Font&, const Colour, UndoManager*, int newCaretPos);
720  void reinsert (int insertIndex, const OwnedArray<UniformTextSection>&);
721  void remove (Range<int> range, UndoManager*, int caretPositionToMoveTo);
722  void getCharPosition (int index, float& x, float& y, float& lineHeight) const;
723  void updateCaretPosition();
724  void updateValueFromText();
725  void textWasChangedByValue();
726  int indexAtPosition (float x, float y);
727  int findWordBreakAfter (int position) const;
728  int findWordBreakBefore (int position) const;
729  bool moveCaretWithTransaction (int newPos, bool selecting);
730  friend class TextHolderComponent;
731  friend class TextEditorViewport;
732  void drawContent (Graphics&);
733  void updateTextHolderSize();
734  float getWordWrapWidth() const;
735  void timerCallbackInt();
736  void repaintText (Range<int>);
737  void scrollByLines (int deltaLines);
738  bool undoOrRedo (bool shouldUndo);
739  UndoManager* getUndoManager() noexcept;
740 
742 };
743 
745 typedef TextEditor::Listener TextEditorListener;
746 
747 
748 #endif // JUCE_TEXTEDITOR_H_INCLUDED
Definition: juce_TextEditor.h:280
Definition: juce_KeyPress.h:37
Definition: juce_TextEditor.cpp:277
Definition: juce_PopupMenu.h:77
static const String empty
Definition: juce_String.h:152
virtual String filterNewText(TextEditor &, const String &newInput)=0
Definition: juce_TextEditor.cpp:754
Definition: juce_Font.h:39
InputFilter * getInputFilter() const noexcept
Definition: juce_TextEditor.h:574
Definition: juce_ApplicationCommandID.h:77
virtual ~InputFilter()
Definition: juce_TextEditor.h:534
#define noexcept
Definition: juce_CompilerSupport.h:141
void setInputRestrictions(int maxTextLength, const String &allowedCharacters=String::empty)
Definition: juce_TextEditor.cpp:1135
Definition: juce_TextEditor.cpp:706
Definition: juce_TextEditor.h:530
InputFilter()
Definition: juce_TextEditor.h:533
Definition: juce_Range.h:44
FocusChangeType
Definition: juce_Component.h:1702
Definition: juce_OptionalScopedPointer.h:44
ColourIds
Definition: juce_TextEditor.h:202
Definition: juce_CaretComponent.h:32
Definition: juce_ApplicationCommandID.h:86
Definition: juce_String.h:43
Definition: juce_ApplicationCommandID.h:71
Definition: juce_TextInputTarget.h:38
#define JUCE_API
Definition: juce_StandardHeader.h:139
#define JUCE_PUBLIC_IN_DLL_BUILD(declaration)
Definition: juce_StandardHeader.h:145
const int borderSize
Definition: juce_gui_basics.cpp:30
#define const
Definition: juce_ApplicationCommandID.h:83
Definition: juce_Rectangle.h:36
Definition: juce_TooltipClient.h:63
Definition: juce_TextEditor.h:594
Definition: juce_Colour.h:35
virtual void textEditorTextChanged(TextEditor &)
Definition: juce_TextEditor.h:287
void setKeyboardType(VirtualKeyboardType type) noexcept
Definition: juce_TextEditor.h:588
Definition: juce_ListenerList.h:69
virtual void textEditorFocusLost(TextEditor &)
Definition: juce_TextEditor.h:296
virtual ~LookAndFeelMethods()
Definition: juce_TextEditor.h:596
Definition: juce_Component.h:33
virtual ~Listener()
Definition: juce_TextEditor.h:284
bool isPopupMenuCurrentlyActive() const noexcept
Definition: juce_TextEditor.h:190
Definition: juce_MouseEvent.h:329
Definition: juce_TextEditor.h:38
bool isCaretVisible() const noexcept
Definition: juce_TextEditor.h:138
png_const_structrp png_const_inforp int png_fixed_point * width
Definition: juce_PNGLoader.cpp:2339
bool areScrollbarsShown() const noexcept
Definition: juce_TextEditor.h:154
VirtualKeyboardType
Definition: juce_TextInputTarget.h:78
Definition: juce_TextEditor.h:546
Definition: juce_TextEditor.cpp:848
Definition: juce_ApplicationCommandID.h:74
Definition: juce_Array.h:60
Definition: juce_BorderSize.h:39
const Font & getFont() const noexcept
Definition: juce_TextEditor.h:248
Definition: juce_OwnedArray.h:55
Definition: juce_Value.h:44
void setInputFilter(InputFilter *newFilter, bool takeOwnership)
Definition: juce_TextEditor.cpp:1130
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
Definition: juce_PlatformDefs.h:198
Definition: juce_GraphicsContext.h:42
virtual void textEditorReturnKeyPressed(TextEditor &)
Definition: juce_TextEditor.h:290
bool getReturnKeyStartsNewLine() const
Definition: juce_TextEditor.h:90
Range< int > getHighlightedRegion() const override
Definition: juce_TextEditor.h:413
VirtualKeyboardType getKeyboardType() override
Definition: juce_TextEditor.h:640
virtual void textEditorEscapeKeyPressed(TextEditor &)
Definition: juce_TextEditor.h:293
Definition: juce_UndoManager.h:49
Definition: juce_TextEditor.cpp:802
juce_wchar getPasswordCharacter() const noexcept
Definition: juce_TextEditor.h:171
Definition: juce_TextEditor.cpp:62
Definition: juce_ApplicationCommandID.h:68
Definition: juce_MouseEvent.h:36
bool isPopupMenuEnabled() const noexcept
Definition: juce_TextEditor.h:187
wchar_t juce_wchar
Definition: juce_CharacterFunctions.h:49
bool isTabKeyUsedAsCharacter() const
Definition: juce_TextEditor.h:103