Public Types | Public Member Functions

CodeEditorComponent Class Reference

A text editor component designed specifically for source code. More...

Inherits Component, TextInputTarget, Timer, ScrollBar::Listener, CodeDocument::Listener, and AsyncUpdater.

List of all members.

Public Types

enum  ColourIds { backgroundColourId = 0x1004500, highlightColourId = 0x1004502, defaultTextColourId = 0x1004503 }
 

A set of colour IDs to use to change the colour of various aspects of the editor.

More...

Public Member Functions

 CodeEditorComponent (CodeDocument &document, CodeTokeniser *codeTokeniser)
 Creates an editor for a document.
 ~CodeEditorComponent ()
 Destructor.
CodeDocumentgetDocument () const noexcept
 Returns the code document that this component is editing.
void loadContent (const String &newContent)
 Loads the given content into the document.
float getCharWidth () const noexcept
 Returns the standard character width.
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 might be visible at the bottom if the component's height isn't an exact multiple of the line-height.
int getNumColumnsOnScreen () const noexcept
 Returns the number of whole columns visible on the screen.
const CodeDocument::Position getCaretPos () const
 Returns the current caret position.
const Rectangle< int > getCaretRectangle ()
 Returns the position of the caret, relative to the editor's origin.
void moveCaretTo (const CodeDocument::Position &newPos, bool selecting)
 Moves the caret.
const Rectangle< int > getCharacterBounds (const CodeDocument::Position &pos) const
 Returns the on-screen position of a character in the document.
const CodeDocument::Position getPositionAt (int x, int y)
 Finds the character at a given on-screen position.
bool moveCaretLeft (bool moveInWholeWordSteps, bool selecting)
bool moveCaretRight (bool moveInWholeWordSteps, bool selecting)
bool moveCaretUp (bool selecting)
bool moveCaretDown (bool selecting)
bool scrollDown ()
bool scrollUp ()
bool pageUp (bool selecting)
bool pageDown (bool selecting)
bool moveCaretToTop (bool selecting)
bool moveCaretToStartOfLine (bool selecting)
bool moveCaretToEnd (bool selecting)
bool moveCaretToEndOfLine (bool selecting)
bool deleteBackwards (bool moveInWholeWordSteps)
bool deleteForwards (bool moveInWholeWordSteps)
bool copyToClipboard ()
bool cutToClipboard ()
bool pasteFromClipboard ()
bool undo ()
bool redo ()
bool selectAll ()
void deselectAll ()
void scrollToLine (int newFirstLineOnScreen)
void scrollBy (int deltaLines)
void scrollToColumn (int newFirstColumnOnScreen)
void scrollToKeepCaretOnScreen ()
void insertTextAtCaret (const String &textToInsert)
 Inserts some text, overwriting the selected text region, if there is one.
void insertTabAtCaret ()
const Range< int > getHighlightedRegion () const
 Returns the extents of the selected text region, or an empty range if nothing is selected,.
void setHighlightedRegion (const Range< int > &newRange)
 Sets the currently-selected text region.
const String getTextInRange (const Range< int > &range) const
 Returns a specified sub-section of the text.
void setTabSize (int numSpacesPerTab, bool insertSpacesInsteadOfTabCharacters)
 Changes the current tab settings.
int getTabSize () const noexcept
 Returns the current number of spaces per tab.
bool areSpacesInsertedForTabs () const
 Returns true if the tab key will insert spaces instead of actual tab characters.
void setFont (const Font &newFont)
 Changes the font.
const FontgetFont () const noexcept
 Returns the font that the editor is using.
void resetToDefaultColours ()
 Resets the syntax highlighting colours to the default ones provided by the code tokeniser.
void setColourForTokenType (int tokenType, const Colour &colour)
 Changes one of the syntax highlighting colours.
const Colour getColourForTokenType (int tokenType) const
 Returns one of the syntax highlighting colours.
void setScrollbarThickness (int thickness)
 Changes the size of the scrollbars.
int getScrollbarThickness () const noexcept
 Returns the thickness of the scrollbars.
void resized ()
 Called when this component's size has been changed.
void paint (Graphics &g)
 Components can override this method to draw their content.
bool keyPressed (const KeyPress &key)
 Called when a key is pressed.
void mouseDown (const MouseEvent &e)
 Called when a mouse button is pressed while it's over this component.
void mouseDrag (const MouseEvent &e)
 Called when the mouse is moved while a button is held down.
void mouseUp (const MouseEvent &e)
 Called when a mouse button is released.
void mouseDoubleClick (const MouseEvent &e)
 Called when a mouse button has been double-clicked in this component.
void mouseWheelMove (const MouseEvent &e, float wheelIncrementX, float wheelIncrementY)
 Called when the mouse-wheel is moved.
void focusGained (FocusChangeType cause)
 Called to indicate that this component has just acquired the keyboard focus.
void focusLost (FocusChangeType cause)
 Called to indicate that this component has just lost the keyboard focus.
void timerCallback ()
 The user-defined callback routine that actually gets called periodically.
void scrollBarMoved (ScrollBar *scrollBarThatHasMoved, double newRangeStart)
 Called when a ScrollBar is moved.
void handleAsyncUpdate ()
 Called back to do whatever your class needs to do.
void codeDocumentChanged (const CodeDocument::Position &affectedTextStart, const CodeDocument::Position &affectedTextEnd)
 Called by a CodeDocument when it is altered.
bool isTextInputActive () const
 Returns true if this input target is currently accepting input.
void setTemporaryUnderlining (const Array< Range< int > > &)
 Sets a number of temporarily underlined sections.

Detailed Description

A text editor component designed specifically for source code.

This is designed to handle syntax highlighting and fast editing of very large files.


Member Enumeration Documentation

A set of colour IDs to use to change the colour of various aspects of the editor.

These constants can be used either via the Component::setColour(), or LookAndFeel::setColour() methods.

See also:
Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
Enumerator:
backgroundColourId 

A colour to use to fill the editor's background.

highlightColourId 

The colour to use for the highlighted background under selected text.

defaultTextColourId 

The colour to use for text when no syntax colouring is enabled.


Constructor & Destructor Documentation

CodeEditorComponent::CodeEditorComponent ( CodeDocument document,
CodeTokeniser codeTokeniser 
)

Creates an editor for a document.

The tokeniser object is optional - pass 0 to disable syntax highlighting. The object that you pass in is not owned or deleted by the editor - you must make sure that it doesn't get deleted while this component is still using it.

See also:
CodeDocument
CodeEditorComponent::~CodeEditorComponent (  )

Destructor.


Member Function Documentation

CodeDocument& CodeEditorComponent::getDocument (  ) const

Returns the code document that this component is editing.

void CodeEditorComponent::loadContent ( const String newContent )

Loads the given content into the document.

This will completely reset the CodeDocument object, clear its undo history, and fill it with this text.

float CodeEditorComponent::getCharWidth (  ) const

Returns the standard character width.

int CodeEditorComponent::getLineHeight (  ) const

Returns the height of a line of text, in pixels.

int CodeEditorComponent::getNumLinesOnScreen (  ) const

Returns the number of whole lines visible on the screen, This doesn't include a cut-off line that might be visible at the bottom if the component's height isn't an exact multiple of the line-height.

int CodeEditorComponent::getNumColumnsOnScreen (  ) const

Returns the number of whole columns visible on the screen.

This doesn't include any cut-off columns at the right-hand edge.

const CodeDocument::Position CodeEditorComponent::getCaretPos (  ) const

Returns the current caret position.

const Rectangle<int> CodeEditorComponent::getCaretRectangle (  ) [virtual]

Returns the position of the caret, relative to the editor's origin.

Implements TextInputTarget.

void CodeEditorComponent::moveCaretTo ( const CodeDocument::Position newPos,
bool  selecting 
)

Moves the caret.

If selecting is true, the section of the document between the current caret position and the new one will become selected. If false, any currently selected region will be deselected.

const Rectangle<int> CodeEditorComponent::getCharacterBounds ( const CodeDocument::Position pos ) const

Returns the on-screen position of a character in the document.

The rectangle returned is relative to this component's top-left origin.

const CodeDocument::Position CodeEditorComponent::getPositionAt ( int  x,
int  y 
)

Finds the character at a given on-screen position.

The co-ordinates are relative to this component's top-left origin.

bool CodeEditorComponent::moveCaretLeft ( bool  moveInWholeWordSteps,
bool  selecting 
)
bool CodeEditorComponent::moveCaretRight ( bool  moveInWholeWordSteps,
bool  selecting 
)
bool CodeEditorComponent::moveCaretUp ( bool  selecting )
bool CodeEditorComponent::moveCaretDown ( bool  selecting )
bool CodeEditorComponent::scrollDown (  )
bool CodeEditorComponent::scrollUp (  )
bool CodeEditorComponent::pageUp ( bool  selecting )
bool CodeEditorComponent::pageDown ( bool  selecting )
bool CodeEditorComponent::moveCaretToTop ( bool  selecting )
bool CodeEditorComponent::moveCaretToStartOfLine ( bool  selecting )
bool CodeEditorComponent::moveCaretToEnd ( bool  selecting )
bool CodeEditorComponent::moveCaretToEndOfLine ( bool  selecting )
bool CodeEditorComponent::deleteBackwards ( bool  moveInWholeWordSteps )
bool CodeEditorComponent::deleteForwards ( bool  moveInWholeWordSteps )
bool CodeEditorComponent::copyToClipboard (  )
bool CodeEditorComponent::cutToClipboard (  )
bool CodeEditorComponent::pasteFromClipboard (  )
bool CodeEditorComponent::undo (  )
bool CodeEditorComponent::redo (  )
bool CodeEditorComponent::selectAll (  )
void CodeEditorComponent::deselectAll (  )
void CodeEditorComponent::scrollToLine ( int  newFirstLineOnScreen )
void CodeEditorComponent::scrollBy ( int  deltaLines )
void CodeEditorComponent::scrollToColumn ( int  newFirstColumnOnScreen )
void CodeEditorComponent::scrollToKeepCaretOnScreen (  )
void CodeEditorComponent::insertTextAtCaret ( const String textToInsert ) [virtual]

Inserts some text, overwriting the selected text region, if there is one.

Implements TextInputTarget.

void CodeEditorComponent::insertTabAtCaret (  )
const Range<int> CodeEditorComponent::getHighlightedRegion (  ) const [virtual]

Returns the extents of the selected text region, or an empty range if nothing is selected,.

Implements TextInputTarget.

void CodeEditorComponent::setHighlightedRegion ( const Range< int > &  newRange ) [virtual]

Sets the currently-selected text region.

Implements TextInputTarget.

const String CodeEditorComponent::getTextInRange ( const Range< int > &  range ) const [virtual]

Returns a specified sub-section of the text.

Implements TextInputTarget.

void CodeEditorComponent::setTabSize ( int  numSpacesPerTab,
bool  insertSpacesInsteadOfTabCharacters 
)

Changes the current tab settings.

This lets you change the tab size and whether pressing the tab key inserts a tab character, or its equivalent number of spaces.

int CodeEditorComponent::getTabSize (  ) const

Returns the current number of spaces per tab.

See also:
setTabSize
bool CodeEditorComponent::areSpacesInsertedForTabs (  ) const

Returns true if the tab key will insert spaces instead of actual tab characters.

See also:
setTabSize
void CodeEditorComponent::setFont ( const Font newFont )

Changes the font.

Make sure you only use a fixed-width font, or this component will look pretty nasty!

const Font& CodeEditorComponent::getFont (  ) const

Returns the font that the editor is using.

void CodeEditorComponent::resetToDefaultColours (  )

Resets the syntax highlighting colours to the default ones provided by the code tokeniser.

See also:
CodeTokeniser::getDefaultColour
void CodeEditorComponent::setColourForTokenType ( int  tokenType,
const Colour colour 
)

Changes one of the syntax highlighting colours.

The token type values are dependent on the tokeniser being used - use CodeTokeniser::getTokenTypes() to get a list of the token types.

See also:
getColourForTokenType
const Colour CodeEditorComponent::getColourForTokenType ( int  tokenType ) const

Returns one of the syntax highlighting colours.

The token type values are dependent on the tokeniser being used - use CodeTokeniser::getTokenTypes() to get a list of the token types.

See also:
setColourForTokenType
void CodeEditorComponent::setScrollbarThickness ( int  thickness )

Changes the size of the scrollbars.

int CodeEditorComponent::getScrollbarThickness (  ) const

Returns the thickness of the scrollbars.

void CodeEditorComponent::resized (  ) [virtual]

Called when this component's size has been changed.

A component can implement this method to do things such as laying out its child components when its width or height changes.

The method is called synchronously as a result of the setBounds or setSize methods, so repeatedly changing a components size will repeatedly call its resized method (unlike things like repainting, where multiple calls to repaint are coalesced together).

If the component is a top-level window on the desktop, its size could also be changed by operating-system factors beyond the application's control.

See also:
moved, setSize

Reimplemented from Component.

void CodeEditorComponent::paint ( Graphics g ) [virtual]

Components can override this method to draw their content.

The paint() method gets called when a region of a component needs redrawing, either because the component's repaint() method has been called, or because something has happened on the screen that means a section of a window needs to be redrawn.

Any child components will draw themselves over whatever this method draws. If you need to paint over the top of your child components, you can also implement the paintOverChildren() method to do this.

If you want to cause a component to redraw itself, this is done asynchronously - calling the repaint() method marks a region of the component as "dirty", and the paint() method will automatically be called sometime later, by the message thread, to paint any bits that need refreshing. In Juce (and almost all modern UI frameworks), you never redraw something synchronously.

You should never need to call this method directly - to take a snapshot of the component you could use createComponentSnapshot() or paintEntireComponent().

Parameters:
gthe graphics context that must be used to do the drawing operations.
See also:
repaint, paintOverChildren, Graphics

Reimplemented from Component.

bool CodeEditorComponent::keyPressed ( const KeyPress key ) [virtual]

Called when a key is pressed.

When a key is pressed, the component that has the keyboard focus will have this method called. Remember that a component will only be given the focus if its setWantsKeyboardFocus() method has been used to enable this.

If your implementation returns true, the event will be consumed and not passed on to any other listeners. If it returns false, the key will be passed to any KeyListeners that have been registered with this component. As soon as one of these returns true, the process will stop, but if they all return false, the event will be passed upwards to this component's parent, and so on.

The default implementation of this method does nothing and returns false.

See also:
keyStateChanged, getCurrentlyFocusedComponent, addKeyListener

Reimplemented from Component.

void CodeEditorComponent::mouseDown ( const MouseEvent e ) [virtual]

Called when a mouse button is pressed while it's over this component.

The MouseEvent object passed in contains lots of methods for finding out which button was pressed, as well as which modifier keys (e.g. shift, ctrl) were held down at the time.

Once a button is held down, the mouseDrag method will be called when the mouse moves, until the button is released.

Parameters:
edetails about the position and status of the mouse event
See also:
mouseUp, mouseDrag, mouseDoubleClick, contains

Reimplemented from Component.

void CodeEditorComponent::mouseDrag ( const MouseEvent e ) [virtual]

Called when the mouse is moved while a button is held down.

When a mouse button is pressed inside a component, that component receives mouseDrag callbacks each time the mouse moves, even if the mouse strays outside the component's bounds.

If you want to be able to drag things off the edge of a component and have the component scroll when you get to the edges, the beginDragAutoRepeat() method might be useful.

Parameters:
edetails about the position and status of the mouse event
See also:
mouseDown, mouseUp, mouseMove, contains, beginDragAutoRepeat

Reimplemented from Component.

void CodeEditorComponent::mouseUp ( const MouseEvent e ) [virtual]

Called when a mouse button is released.

A mouseUp callback is sent to the component in which a button was pressed even if the mouse is actually over a different component when the button is released.

The MouseEvent object passed in contains lots of methods for finding out which buttons were down just before they were released.

Parameters:
edetails about the position and status of the mouse event
See also:
mouseDown, mouseDrag, mouseDoubleClick, contains

Reimplemented from Component.

void CodeEditorComponent::mouseDoubleClick ( const MouseEvent e ) [virtual]

Called when a mouse button has been double-clicked in this component.

The MouseEvent object passed in contains lots of methods for finding out which button was pressed, as well as which modifier keys (e.g. shift, ctrl) were held down at the time.

For altering the time limit used to detect double-clicks, see MouseEvent::setDoubleClickTimeout.

Parameters:
edetails about the position and status of the mouse event
See also:
mouseDown, mouseUp, MouseEvent::setDoubleClickTimeout, MouseEvent::getDoubleClickTimeout

Reimplemented from Component.

void CodeEditorComponent::mouseWheelMove ( const MouseEvent e,
float  wheelIncrementX,
float  wheelIncrementY 
) [virtual]

Called when the mouse-wheel is moved.

This callback is sent to the component that the mouse is over when the wheel is moved.

If not overridden, the component will forward this message to its parent, so that parent components can collect mouse-wheel messages that happen to child components which aren't interested in them.

Parameters:
edetails about the position and status of the mouse event
wheelIncrementXthe speed and direction of the horizontal scroll-wheel - a positive value means the wheel has been pushed to the right, negative means it was pushed to the left
wheelIncrementYthe speed and direction of the vertical scroll-wheel - a positive value means the wheel has been pushed upwards, negative means it was pushed downwards

Reimplemented from Component.

void CodeEditorComponent::focusGained ( FocusChangeType  cause ) [virtual]

Called to indicate that this component has just acquired the keyboard focus.

See also:
focusLost, setWantsKeyboardFocus, getCurrentlyFocusedComponent, hasKeyboardFocus

Reimplemented from Component.

void CodeEditorComponent::focusLost ( FocusChangeType  cause ) [virtual]

Called to indicate that this component has just lost the keyboard focus.

See also:
focusGained, setWantsKeyboardFocus, getCurrentlyFocusedComponent, hasKeyboardFocus

Reimplemented from Component.

void CodeEditorComponent::timerCallback (  ) [virtual]

The user-defined callback routine that actually gets called periodically.

It's perfectly ok to call startTimer() or stopTimer() from within this callback to change the subsequent intervals.

Implements Timer.

void CodeEditorComponent::scrollBarMoved ( ScrollBar scrollBarThatHasMoved,
double  newRangeStart 
) [virtual]

Called when a ScrollBar is moved.

Parameters:
scrollBarThatHasMovedthe bar that has moved
newRangeStartthe new range start of this bar

Implements ScrollBar::Listener.

void CodeEditorComponent::handleAsyncUpdate (  ) [virtual]

Called back to do whatever your class needs to do.

This method is called by the message thread at the next convenient time after the triggerAsyncUpdate() method has been called.

Implements AsyncUpdater.

void CodeEditorComponent::codeDocumentChanged ( const CodeDocument::Position affectedTextStart,
const CodeDocument::Position affectedTextEnd 
) [virtual]

Called by a CodeDocument when it is altered.

Implements CodeDocument::Listener.

bool CodeEditorComponent::isTextInputActive (  ) const [virtual]

Returns true if this input target is currently accepting input.

For example, a text editor might return false if it's in read-only mode.

Implements TextInputTarget.

void CodeEditorComponent::setTemporaryUnderlining ( const Array< Range< int > > &  underlinedRegions ) [virtual]

Sets a number of temporarily underlined sections.

This is needed by MS Windows input method UI.

Implements TextInputTarget.


The documentation for this class was generated from the following file:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines