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_CodeEditorComponent.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
29class CodeTokeniser;
30
31
32//==============================================================================
41class JUCE_API CodeEditorComponent : public TextInputTarget,
42 public Component,
44{
45public:
46 //==============================================================================
56 CodeTokeniser* codeTokeniser);
57
59 ~CodeEditorComponent() override;
60
61 //==============================================================================
63 CodeDocument& getDocument() const noexcept { return document; }
64
69 void loadContent (const String& newContent);
70
71 //==============================================================================
73 float getCharWidth() const noexcept { return charWidth; }
74
76 int getLineHeight() const noexcept { return lineHeight; }
77
82 int getNumLinesOnScreen() const noexcept { return linesOnScreen; }
83
85 int getFirstLineOnScreen() const noexcept { return firstLineOnScreen; }
86
90 int getNumColumnsOnScreen() const noexcept { return columnsOnScreen; }
91
93 CodeDocument::Position getCaretPos() const { return caretPos; }
94
96 int getTotalNumChars() const override { return document.getNumCharacters(); }
97
103 void moveCaretTo (const CodeDocument::Position& newPos, bool selecting);
104
108 Rectangle<int> getCharacterBounds (const CodeDocument::Position& pos) const;
109
113 CodeDocument::Position getPositionAt (int x, int y) const;
114
116 CodeDocument::Position getSelectionStart() const { return selectionStart; }
117
119 CodeDocument::Position getSelectionEnd() const { return selectionEnd; }
120
122 void setLineNumbersShown (bool shouldBeShown);
123
125 int getCaretPosition() const override { return getCaretPos().getPosition(); }
126
128 int getCharIndexForPoint (Point<int> point) const override;
129
132 {
133 return getCharacterBounds ({ document, index });
134 }
135
142 RectangleList<int> getTextBounds (Range<int> textRange) const override;
143
144 //==============================================================================
145 bool moveCaretLeft (bool moveInWholeWordSteps, bool selecting);
146 bool moveCaretRight (bool moveInWholeWordSteps, bool selecting);
147 bool moveCaretUp (bool selecting);
148 bool moveCaretDown (bool selecting);
149 bool scrollDown();
150 bool scrollUp();
151 bool pageUp (bool selecting);
152 bool pageDown (bool selecting);
153 bool moveCaretToTop (bool selecting);
154 bool moveCaretToStartOfLine (bool selecting);
155 bool moveCaretToEnd (bool selecting);
156 bool moveCaretToEndOfLine (bool selecting);
157 bool deleteBackwards (bool moveInWholeWordSteps);
158 bool deleteForwards (bool moveInWholeWordSteps);
159 bool deleteWhitespaceBackwardsToTabStop();
160 virtual bool copyToClipboard();
161 virtual bool cutToClipboard();
162 virtual bool pasteFromClipboard();
163 bool undo();
164 bool redo();
165
166 void selectRegion (const CodeDocument::Position& start, const CodeDocument::Position& end);
167 bool selectAll();
168 void deselectAll();
169
170 void scrollToLine (int newFirstLineOnScreen);
171 void scrollBy (int deltaLines);
172 void scrollToColumn (int newFirstColumnOnScreen);
173 void scrollToKeepCaretOnScreen();
174 void scrollToKeepLinesOnScreen (Range<int> linesToShow);
175
176 void insertTextAtCaret (const String& textToInsert) override;
177 void insertTabAtCaret();
178
179 void indentSelection();
180 void unindentSelection();
181
182 //==============================================================================
183 Range<int> getHighlightedRegion() const override;
184 bool isHighlightActive() const noexcept;
185 void setHighlightedRegion (const Range<int>& newRange) override;
186 String getTextInRange (const Range<int>& range) const override;
187
188 //==============================================================================
190 struct State
191 {
193 State (const CodeEditorComponent&);
195 State (const String& stringifiedVersion);
196 State (const State&) noexcept;
197
199 void restoreState (CodeEditorComponent&) const;
200
202 String toString() const;
203
204 private:
205 int lastTopLine, lastCaretPos, lastSelectionEnd;
206 };
207
208 //==============================================================================
213 void setTabSize (int numSpacesPerTab, bool insertSpacesInsteadOfTabCharacters);
214
218 int getTabSize() const noexcept { return spacesPerTab; }
219
223 bool areSpacesInsertedForTabs() const { return useSpacesForTabs; }
224
226 String getTabString (int numSpaces) const;
227
231 void setFont (const Font& newFont);
232
234 const Font& getFont() const noexcept { return font; }
235
237 void setReadOnly (bool shouldBeReadOnly) noexcept;
238
240 bool isReadOnly() const noexcept { return readOnly; }
241
242 //==============================================================================
244 struct JUCE_API ColourScheme
245 {
248 {
249 String name;
250 Colour colour;
251 };
252
253 Array<TokenType> types;
254
255 void set (const String& name, Colour colour);
256 };
257
263 void setColourScheme (const ColourScheme& scheme);
264
266 const ColourScheme& getColourScheme() const noexcept { return colourScheme; }
267
272 Colour getColourForTokenType (int tokenType) const;
273
285 void retokenise (int startIndex, int endIndex);
286
287 //==============================================================================
296 {
297 backgroundColourId = 0x1004500,
298 highlightColourId = 0x1004502,
299 defaultTextColourId = 0x1004503,
300 lineNumberBackgroundId = 0x1004504,
301 lineNumberTextId = 0x1004505,
302 };
303
304 //==============================================================================
306 void setScrollbarThickness (int thickness);
307
309 int getScrollbarThickness() const noexcept { return scrollbarThickness; }
310
311 //==============================================================================
313 virtual void handleReturnKey();
315 virtual void handleTabKey();
317 virtual void handleEscapeKey();
318
320 virtual void editorViewportPositionChanged();
321
323 virtual void caretPositionMoved();
324
325 //==============================================================================
343 virtual void addPopupMenuItems (PopupMenu& menuToAddTo,
344 const MouseEvent* mouseClickEvent);
345
357 virtual void performPopupMenuAction (int menuItemID);
358
366 void setCommandManager (ApplicationCommandManager* newManager) noexcept;
367
368 //==============================================================================
370 void paint (Graphics&) override;
372 void resized() override;
374 bool keyPressed (const KeyPress&) override;
376 void mouseDown (const MouseEvent&) override;
378 void mouseDrag (const MouseEvent&) override;
380 void mouseUp (const MouseEvent&) override;
382 void mouseDoubleClick (const MouseEvent&) override;
384 void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
386 void focusGained (FocusChangeType) override;
388 void focusLost (FocusChangeType) override;
390 bool isTextInputActive() const override;
392 void setTemporaryUnderlining (const Array<Range<int>>&) override;
394 ApplicationCommandTarget* getNextCommandTarget() override;
396 void getAllCommands (Array<CommandID>&) override;
398 void getCommandInfo (CommandID, ApplicationCommandInfo&) override;
400 bool perform (const InvocationInfo&) override;
402 void lookAndFeelChanged() override;
404 std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
405
406private:
407 //==============================================================================
408 CodeDocument& document;
409
410 Font font;
411 int firstLineOnScreen = 0, spacesPerTab = 4;
412 float charWidth = 0;
413 int lineHeight = 0, linesOnScreen = 0, columnsOnScreen = 0;
414 int scrollbarThickness = 16, columnToTryToMaintain = -1;
415 bool readOnly = false, useSpacesForTabs = true, showLineNumbers = false, shouldFollowDocumentChanges = false;
416 double xOffset = 0;
417 CodeDocument::Position caretPos, selectionStart, selectionEnd;
418
420 ScrollBar verticalScrollBar { true }, horizontalScrollBar { false };
421 ApplicationCommandManager* appCommandManager = nullptr;
422
423 class Pimpl;
425
426 class GutterComponent;
428
429 class CodeEditorAccessibilityHandler;
430
431 enum DragType
432 {
433 notDragging,
434 draggingSelectionStart,
435 draggingSelectionEnd
436 };
437
438 DragType dragType = notDragging;
439
440 //==============================================================================
441 CodeTokeniser* codeTokeniser;
442 ColourScheme colourScheme;
443
444 class CodeEditorLine;
445 OwnedArray<CodeEditorLine> lines;
446 void rebuildLineTokens();
447 void rebuildLineTokensAsync();
448 void codeDocumentChanged (int start, int end);
449
450 Array<CodeDocument::Iterator> cachedIterators;
451 void clearCachedIterators (int firstLineToBeInvalid);
452 void updateCachedIterators (int maxLineNum);
453 void getIteratorForPosition (int position, CodeDocument::Iterator&);
454
455 void moveLineDelta (int delta, bool selecting);
456 int getGutterSize() const noexcept;
457
458 //==============================================================================
459 void insertText (const String&);
460 virtual void updateCaretPosition();
461 void updateScrollBars();
462 void scrollToLineInternal (int line);
463 void scrollToColumnInternal (double column);
464 void newTransaction();
465 void cut();
466 void indentSelectedLines (int spacesToAdd);
467 bool skipBackwardsToPreviousTab();
468 bool performCommand (CommandID);
469 void setSelection (CodeDocument::Position, CodeDocument::Position);
470
471 int indexToColumn (int line, int index) const noexcept;
472 int columnToIndex (int line, int column) const noexcept;
473
475};
476
477} // namespace juce
One of these objects holds a list of all the commands your app can perform, and despatches these comm...
A command target publishes a list of command IDs that it can perform.
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
A position in a code document.
A class for storing and manipulating a source code file.
int getNumCharacters() const noexcept
Returns the number of characters in the document.
A text editor component designed specifically for source code.
int getFirstLineOnScreen() const noexcept
Returns the index of the first line that's visible at the top of the editor.
Rectangle< int > getCaretRectangleForCharIndex(int index) const override
Returns the bounds of the caret at a particular location in the text.
int getLineHeight() const noexcept
Returns the height of a line of text, in pixels.
int getNumLinesOnScreen() const noexcept
Returns the number of whole lines visible on the screen, This doesn't include a cut-off line that mig...
int getTotalNumChars() const override
Returns the total number of codepoints in the string.
bool areSpacesInsertedForTabs() const
Returns true if the tab key will insert spaces instead of actual tab characters.
CodeDocument::Position getCaretPos() const
Returns the current caret position.
float getCharWidth() const noexcept
Returns the standard character width.
int getScrollbarThickness() const noexcept
Returns the thickness of the scrollbars.
int getCaretPosition() const override
Returns the number of characters from the beginning of the document to the caret.
const ColourScheme & getColourScheme() const noexcept
Returns the current syntax highlighting colour scheme.
CodeDocument & getDocument() const noexcept
Returns the code document that this component is editing.
int getNumColumnsOnScreen() const noexcept
Returns the number of whole columns visible on the screen.
int getTabSize() const noexcept
Returns the current number of spaces per tab.
CodeDocument::Position getSelectionStart() const
Returns the start of the selection as a position.
CodeDocument::Position getSelectionEnd() const
Returns the end of the selection as a position.
bool isReadOnly() const noexcept
Returns true if the editor is set to be read-only.
const Font & getFont() const noexcept
Returns the font that the editor is using.
ColourIds
A set of colour IDs to use to change the colour of various aspects of the editor.
A base class for tokenising code so that the syntax can be displayed in a code editor.
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
The base class for all JUCE user-interface objects.
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 key press, including any modifier keys that are needed.
Contains position and status information about a mouse event.
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.
A scrollbar component.
The JUCE String class!
Definition juce_String.h:53
An abstract base class which can be implemented by components that function as text editors.
#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.
int CommandID
A type used to hold the unique ID for an application command.
Contains status information about a mouse wheel event.
Holds information describing an application command.
Defines a syntax highlighting colour scheme.
Can be used to save and restore the editor's caret position, selection state, etc.