openshot-audio  0.1.7
juce_CodeEditorComponent.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_CODEEDITORCOMPONENT_H_INCLUDED
26 #define JUCE_CODEEDITORCOMPONENT_H_INCLUDED
27 
28 class CodeTokeniser;
29 
30 
31 //==============================================================================
40  public TextInputTarget
41 {
42 public:
43  //==============================================================================
53  CodeTokeniser* codeTokeniser);
54 
57 
58  //==============================================================================
60  CodeDocument& getDocument() const noexcept { return document; }
61 
66  void loadContent (const String& newContent);
67 
68  //==============================================================================
70  float getCharWidth() const noexcept { return charWidth; }
71 
73  int getLineHeight() const noexcept { return lineHeight; }
74 
79  int getNumLinesOnScreen() const noexcept { return linesOnScreen; }
80 
82  int getFirstLineOnScreen() const noexcept { return firstLineOnScreen; }
83 
87  int getNumColumnsOnScreen() const noexcept { return columnsOnScreen; }
88 
90  CodeDocument::Position getCaretPos() const { return caretPos; }
91 
94 
100  void moveCaretTo (const CodeDocument::Position& newPos, bool selecting);
101 
105  Rectangle<int> getCharacterBounds (const CodeDocument::Position& pos) const;
106 
110  CodeDocument::Position getPositionAt (int x, int y);
111 
113  CodeDocument::Position getSelectionStart() const { return selectionStart; }
114 
116  CodeDocument::Position getSelectionEnd() const { return selectionEnd; }
117 
119  void setLineNumbersShown (bool shouldBeShown);
120 
121  //==============================================================================
122  bool moveCaretLeft (bool moveInWholeWordSteps, bool selecting);
123  bool moveCaretRight (bool moveInWholeWordSteps, bool selecting);
124  bool moveCaretUp (bool selecting);
125  bool moveCaretDown (bool selecting);
126  bool scrollDown();
127  bool scrollUp();
128  bool pageUp (bool selecting);
129  bool pageDown (bool selecting);
130  bool moveCaretToTop (bool selecting);
131  bool moveCaretToStartOfLine (bool selecting);
132  bool moveCaretToEnd (bool selecting);
133  bool moveCaretToEndOfLine (bool selecting);
134  bool deleteBackwards (bool moveInWholeWordSteps);
135  bool deleteForwards (bool moveInWholeWordSteps);
136  bool deleteWhitespaceBackwardsToTabStop();
137  virtual bool copyToClipboard();
138  virtual bool cutToClipboard();
139  virtual bool pasteFromClipboard();
140  bool undo();
141  bool redo();
142 
143  void selectRegion (const CodeDocument::Position& start, const CodeDocument::Position& end);
144  bool selectAll();
145  void deselectAll();
146 
147  void scrollToLine (int newFirstLineOnScreen);
148  void scrollBy (int deltaLines);
149  void scrollToColumn (int newFirstColumnOnScreen);
150  void scrollToKeepCaretOnScreen();
151  void scrollToKeepLinesOnScreen (Range<int> linesToShow);
152 
153  void insertTextAtCaret (const String& textToInsert) override;
154  void insertTabAtCaret();
155 
156  void indentSelection();
157  void unindentSelection();
158 
159  //==============================================================================
160  Range<int> getHighlightedRegion() const override;
161  bool isHighlightActive() const noexcept;
162  void setHighlightedRegion (const Range<int>& newRange) override;
163  String getTextInRange (const Range<int>& range) const override;
164 
165  //==============================================================================
167  struct State
168  {
170  State (const CodeEditorComponent&);
172  State (const String& stringifiedVersion);
173  State (const State&) noexcept;
174 
176  void restoreState (CodeEditorComponent&) const;
177 
179  String toString() const;
180 
181  private:
182  int lastTopLine, lastCaretPos, lastSelectionEnd;
183  };
184 
185  //==============================================================================
190  void setTabSize (int numSpacesPerTab, bool insertSpacesInsteadOfTabCharacters);
191 
195  int getTabSize() const noexcept { return spacesPerTab; }
196 
200  bool areSpacesInsertedForTabs() const { return useSpacesForTabs; }
201 
203  String getTabString (int numSpaces) const;
204 
208  void setFont (const Font& newFont);
209 
211  const Font& getFont() const noexcept { return font; }
212 
214  void setReadOnly (bool shouldBeReadOnly) noexcept;
215 
217  bool isReadOnly() const noexcept { return readOnly; }
218 
219  //==============================================================================
221  {
222  struct TokenType
223  {
226  };
227 
229 
230  void set (const String& name, const Colour colour);
231  };
232 
238  void setColourScheme (const ColourScheme& scheme);
239 
241  const ColourScheme& getColourScheme() const noexcept { return colourScheme; }
242 
247  Colour getColourForTokenType (int tokenType) const;
248 
249  //==============================================================================
258  {
259  backgroundColourId = 0x1004500,
260  highlightColourId = 0x1004502,
261  defaultTextColourId = 0x1004503,
262  lineNumberBackgroundId = 0x1004504,
263  lineNumberTextId = 0x1004505,
264  };
265 
266  //==============================================================================
268  void setScrollbarThickness (int thickness);
269 
271  int getScrollbarThickness() const noexcept { return scrollbarThickness; }
272 
273  //==============================================================================
275  virtual void handleReturnKey();
277  virtual void handleTabKey();
279  virtual void handleEscapeKey();
280 
281  //==============================================================================
299  virtual void addPopupMenuItems (PopupMenu& menuToAddTo,
300  const MouseEvent* mouseClickEvent);
301 
313  virtual void performPopupMenuAction (int menuItemID);
314 
322  void setCommandManager (ApplicationCommandManager* newManager) noexcept;
323 
324  //==============================================================================
326  void paint (Graphics&) override;
328  void resized() override;
330  bool keyPressed (const KeyPress&) override;
332  void mouseDown (const MouseEvent&) override;
334  void mouseDrag (const MouseEvent&) override;
336  void mouseUp (const MouseEvent&) override;
338  void mouseDoubleClick (const MouseEvent&) override;
340  void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
342  void focusGained (FocusChangeType) override;
344  void focusLost (FocusChangeType) override;
346  bool isTextInputActive() const override;
348  void setTemporaryUnderlining (const Array<Range<int> >&) override;
350  ApplicationCommandTarget* getNextCommandTarget() override;
352  void getAllCommands (Array<CommandID>&) override;
354  void getCommandInfo (CommandID, ApplicationCommandInfo&) override;
356  bool perform (const InvocationInfo&) override;
357 
358 private:
359  //==============================================================================
360  CodeDocument& document;
361 
362  Font font;
363  int firstLineOnScreen, spacesPerTab;
364  float charWidth;
365  int lineHeight, linesOnScreen, columnsOnScreen;
366  int scrollbarThickness, columnToTryToMaintain;
367  bool readOnly, useSpacesForTabs, showLineNumbers, shouldFollowDocumentChanges;
368  double xOffset;
369 
370  CodeDocument::Position caretPos, selectionStart, selectionEnd;
371 
373  ScrollBar verticalScrollBar, horizontalScrollBar;
374  ApplicationCommandManager* appCommandManager;
375 
376  class Pimpl;
377  friend class Pimpl;
378  friend struct ContainerDeletePolicy<Pimpl>;
379  ScopedPointer<Pimpl> pimpl;
380 
382  friend class GutterComponent;
385 
386  enum DragType
387  {
388  notDragging,
389  draggingSelectionStart,
390  draggingSelectionEnd
391  };
392 
393  DragType dragType;
394 
395  //==============================================================================
396  CodeTokeniser* codeTokeniser;
397  ColourScheme colourScheme;
398 
399  class CodeEditorLine;
401  void rebuildLineTokens();
402  void rebuildLineTokensAsync();
403  void codeDocumentChanged (int start, int end);
404 
405  OwnedArray<CodeDocument::Iterator> cachedIterators;
406  void clearCachedIterators (int firstLineToBeInvalid);
407  void updateCachedIterators (int maxLineNum);
408  void getIteratorForPosition (int position, CodeDocument::Iterator&);
409 
410  void moveLineDelta (int delta, bool selecting);
411  int getGutterSize() const noexcept;
412 
413  //==============================================================================
414  void insertText (const String&);
415  virtual void updateCaretPosition();
416  void updateScrollBars();
417  void scrollToLineInternal (int line);
418  void scrollToColumnInternal (double column);
419  void newTransaction();
420  void cut();
421  void indentSelectedLines (int spacesToAdd);
422  bool skipBackwardsToPreviousTab();
423  bool performCommand (CommandID);
424 
425  int indexToColumn (int line, int index) const noexcept;
426  int columnToIndex (int line, int column) const noexcept;
427 
429 };
430 
431 
432 #endif // JUCE_CODEEDITORCOMPONENT_H_INCLUDED
int getTabSize() const noexcept
Definition: juce_CodeEditorComponent.h:195
Definition: juce_ApplicationCommandManager.h:84
virtual Rectangle< int > getCaretRectangle()=0
Definition: juce_KeyPress.h:37
Definition: juce_PopupMenu.h:77
Definition: juce_CodeEditorComponent.h:167
virtual void insertTextAtCaret(const String &textToInsert)=0
Definition: juce_Font.h:39
Definition: juce_CodeEditorComponent.cpp:25
int getNumLinesOnScreen() const noexcept
Definition: juce_CodeEditorComponent.h:79
#define noexcept
Definition: juce_CompilerSupport.h:141
Colour colour
Definition: juce_CodeEditorComponent.h:225
Definition: juce_ScrollBar.h:49
Definition: juce_CodeEditorComponent.cpp:247
bool areSpacesInsertedForTabs() const
Definition: juce_CodeEditorComponent.h:200
Definition: juce_Range.h:44
ColourIds
Definition: juce_CodeEditorComponent.h:257
CodeDocument & getDocument() const noexcept
Definition: juce_CodeEditorComponent.h:60
const ColourScheme & getColourScheme() const noexcept
Definition: juce_CodeEditorComponent.h:241
Definition: juce_CodeDocument.h:59
FocusChangeType
Definition: juce_Component.h:1702
Definition: juce_CodeDocument.h:354
CodeDocument::Position getSelectionEnd() const
Definition: juce_CodeEditorComponent.h:116
Definition: juce_String.h:43
Definition: juce_TextInputTarget.h:38
Definition: juce_CodeEditorComponent.h:220
const Font & getFont() const noexcept
Definition: juce_CodeEditorComponent.h:211
#define JUCE_API
Definition: juce_StandardHeader.h:139
Definition: juce_ApplicationCommandTarget.h:58
CodeDocument::Position getSelectionStart() const
Definition: juce_CodeEditorComponent.h:113
Definition: juce_Rectangle.h:36
virtual void setHighlightedRegion(const Range< int > &newRange)=0
Definition: juce_Colour.h:35
Definition: juce_ApplicationCommandTarget.h:44
Definition: juce_CodeDocument.h:42
Definition: juce_Component.h:33
CodeDocument::Position getCaretPos() const
Definition: juce_CodeEditorComponent.h:90
Definition: juce_MouseEvent.h:329
Definition: juce_CodeEditorComponent.h:222
int getNumColumnsOnScreen() const noexcept
Definition: juce_CodeEditorComponent.h:87
bool isReadOnly() const noexcept
Definition: juce_CodeEditorComponent.h:217
Definition: juce_CodeTokeniser.h:36
Definition: juce_ContainerDeletePolicy.h:44
Definition: juce_ApplicationCommandID.h:83
float getCharWidth() const noexcept
Definition: juce_CodeEditorComponent.h:70
Definition: juce_Array.h:60
int getFirstLineOnScreen() const noexcept
Definition: juce_CodeEditorComponent.h:82
int CommandID
Definition: juce_ApplicationCommandID.h:37
Definition: juce_ApplicationCommandID.h:86
Definition: juce_OwnedArray.h:55
Definition: juce_ApplicationCommandID.h:80
Definition: juce_CodeEditorComponent.h:38
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
Definition: juce_PlatformDefs.h:198
Definition: juce_GraphicsContext.h:42
Array< TokenType > types
Definition: juce_CodeEditorComponent.h:228
Definition: juce_ApplicationCommandID.h:77
Definition: juce_ApplicationCommandID.h:68
int getScrollbarThickness() const noexcept
Definition: juce_CodeEditorComponent.h:271
Definition: juce_CodeEditorComponent.cpp:283
Definition: juce_ApplicationCommandInfo.h:42
int getLineHeight() const noexcept
Definition: juce_CodeEditorComponent.h:73
virtual Range< int > getHighlightedRegion() const =0
String name
Definition: juce_CodeEditorComponent.h:224
Definition: juce_MouseEvent.h:36
virtual String getTextInRange(const Range< int > &range) const =0