A component that displays a strip of column headings for a table, and allows these to be resized, dragged around, etc. More...
Inherits Component, and AsyncUpdater.
Classes | |
| class | Listener |
| Receives events from a TableHeaderComponent when columns are resized, moved, etc. More... | |
Public Types | |
| enum | ColumnPropertyFlags { visible = 1, resizable = 2, draggable = 4, appearsOnColumnMenu = 8, sortable = 16, sortedForwards = 32, sortedBackwards = 64, defaultFlags = (visible | resizable | draggable | appearsOnColumnMenu | sortable), notResizable = (visible | draggable | appearsOnColumnMenu | sortable), notResizableOrSortable = (visible | draggable | appearsOnColumnMenu), notSortable = (visible | resizable | draggable | appearsOnColumnMenu) } |
A combination of these flags are passed into the addColumn() method to specify the properties of a column. More... | |
Public Member Functions | |
| TableHeaderComponent () | |
| Creates an empty table header. | |
| ~TableHeaderComponent () | |
| Destructor. | |
| void | addColumn (const String &columnName, int columnId, int width, int minimumWidth=30, int maximumWidth=-1, int propertyFlags=defaultFlags, int insertIndex=-1) |
| Adds a column to the table. | |
| void | removeColumn (int columnIdToRemove) |
| Removes a column with the given ID. | |
| void | removeAllColumns () |
| Deletes all columns from the table. | |
| int | getNumColumns (bool onlyCountVisibleColumns) const |
| Returns the number of columns in the table. | |
| String | getColumnName (int columnId) const |
| Returns the name for a column. | |
| void | setColumnName (int columnId, const String &newName) |
| Changes the name of a column. | |
| void | moveColumn (int columnId, int newVisibleIndex) |
| Moves a column to a different index in the table. | |
| int | getColumnWidth (int columnId) const |
| Returns the width of one of the columns. | |
| void | setColumnWidth (int columnId, int newWidth) |
| Changes the width of a column. | |
| void | setColumnVisible (int columnId, bool shouldBeVisible) |
| Shows or hides a column. | |
| bool | isColumnVisible (int columnId) const |
| Returns true if this column is currently visible. | |
| void | setSortColumnId (int columnId, bool sortForwards) |
| Changes the column which is the sort column. | |
| int | getSortColumnId () const |
| Returns the column ID by which the table is currently sorted, or 0 if it is unsorted. | |
| bool | isSortedForwards () const |
| Returns true if the table is currently sorted forwards, or false if it's backwards. | |
| void | reSortTable () |
| Triggers a re-sort of the table according to the current sort-column. | |
| int | getTotalWidth () const |
| Returns the total width of all the visible columns in the table. | |
| int | getIndexOfColumnId (int columnId, bool onlyCountVisibleColumns) const |
| Returns the index of a given column. | |
| int | getColumnIdOfIndex (int index, bool onlyCountVisibleColumns) const |
| Returns the ID of the column at a given index. | |
| const Rectangle< int > | getColumnPosition (int index) const |
| Returns the rectangle containing of one of the columns. | |
| int | getColumnIdAtX (int xToFind) const |
| Finds the column ID at a given x-position in the component. | |
| void | setStretchToFitActive (bool shouldStretchToFit) |
| If set to true, this indicates that the columns should be expanded or shrunk to fill the entire width of the component. | |
| bool | isStretchToFitActive () const |
| Returns true if stretch-to-fit has been enabled. | |
| void | resizeAllColumnsToFit (int targetTotalWidth) |
| If stretch-to-fit is enabled, this will resize all the columns to make them fit into the specified width, keeping their relative proportions the same. | |
| void | setPopupMenuActive (bool hasMenu) |
| Enables or disables the pop-up menu. | |
| bool | isPopupMenuActive () const |
| Returns true if the pop-up menu is enabled. | |
| String | toString () const |
| Returns a string that encapsulates the table's current layout. | |
| void | restoreFromString (const String &storedVersion) |
| Restores the state of the table, based on a string previously created with toString(). | |
| void | addListener (Listener *newListener) |
| Adds a listener to be informed about things that happen to the header. | |
| void | removeListener (Listener *listenerToRemove) |
| Removes a previously-registered listener. | |
| virtual void | columnClicked (int columnId, const ModifierKeys &mods) |
| This can be overridden to handle a mouse-click on one of the column headers. | |
| virtual void | addMenuItems (PopupMenu &menu, int columnIdClicked) |
| This can be overridden to add custom items to the pop-up menu. | |
| virtual void | reactToMenuItem (int menuReturnId, int columnIdClicked) |
| Override this to handle any custom items that you have added to the pop-up menu with an addMenuItems() override. | |
| void | paint (Graphics &g) |
| Components can override this method to draw their content. | |
| void | resized () |
| Called when this component's size has been changed. | |
| void | mouseMove (const MouseEvent &) |
| Called when the mouse moves inside this component. | |
| void | mouseEnter (const MouseEvent &) |
| Called when the mouse first enters this component. | |
| void | mouseExit (const MouseEvent &) |
| Called when the mouse moves out of this component. | |
| void | mouseDown (const MouseEvent &) |
| Called when a mouse button is pressed while it's over this component. | |
| void | mouseDrag (const MouseEvent &) |
| Called when the mouse is moved while a button is held down. | |
| void | mouseUp (const MouseEvent &) |
| Called when a mouse button is released. | |
| const MouseCursor | getMouseCursor () |
| Returns the mouse cursor shape to use when the mouse is over this component. | |
| virtual void | showColumnChooserMenu (int columnIdClicked) |
| Can be overridden for more control over the pop-up menu behaviour. | |
A component that displays a strip of column headings for a table, and allows these to be resized, dragged around, etc.
This is just the component that goes at the top of a table. You can use it directly for custom components, or to create a simple table, use the TableListBox class.
To use one of these, create it and use addColumn() to add all the columns that you need. Each column must be given a unique ID number that's used to refer to it.
A combination of these flags are passed into the addColumn() method to specify the properties of a column.
| visible |
If this is set, the column will be shown; if not, it will be hidden until the user enables it with the pop-up menu. |
| resizable |
If this is set, the column can be resized by dragging it. |
| draggable |
If this is set, the column can be dragged around to change its order in the table. |
| appearsOnColumnMenu |
If this is set, the column will be shown on the pop-up menu allowing it to be hidden/shown. |
| sortable |
If this is set, then clicking on the column header will set it to be the sort column, and clicking again will reverse the order. |
| sortedForwards |
If this is set, the column is currently the one by which the table is sorted (forwards). |
| sortedBackwards |
If this is set, the column is currently the one by which the table is sorted (backwards). |
| defaultFlags |
This set of default flags is used as the default parameter value in addColumn(). |
| notResizable |
A quick way of combining flags for a column that's not resizable. |
| notResizableOrSortable |
A quick way of combining flags for a column that's not resizable or sortable. |
| notSortable |
A quick way of combining flags for a column that's not sortable. |
| TableHeaderComponent::TableHeaderComponent | ( | ) |
Creates an empty table header.
| TableHeaderComponent::~TableHeaderComponent | ( | ) |
Destructor.
| void TableHeaderComponent::addColumn | ( | const String & | columnName, |
| int | columnId, | ||
| int | width, | ||
| int | minimumWidth = 30, |
||
| int | maximumWidth = -1, |
||
| int | propertyFlags = defaultFlags, |
||
| int | insertIndex = -1 |
||
| ) |
Adds a column to the table.
This will add a column, and asynchronously call the tableColumnsChanged() method of any registered listeners.
| columnName | the name of the new column. It's ok to have two or more columns with the same name |
| columnId | an ID for this column. The ID can be any number apart from 0, but every column must have a unique ID. This is used to identify the column later on, after the user may have changed the order that they appear in |
| width | the initial width of the column, in pixels |
| maximumWidth | a maximum width that the column can take when the user is resizing it. This only applies if the 'resizable' flag is specified for this column |
| minimumWidth | a minimum width that the column can take when the user is resizing it. This only applies if the 'resizable' flag is specified for this column |
| propertyFlags | a combination of some of the values from the ColumnPropertyFlags enum, to define the properties of this column |
| insertIndex | the index at which the column should be added. A value of 0 puts it at the start (left-hand side) and -1 puts it at the end (right-hand size) of the table. Note that the index the index within all columns, not just the index amongst those that are currently visible |
| void TableHeaderComponent::removeColumn | ( | int | columnIdToRemove ) |
Removes a column with the given ID.
If there is such a column, this will asynchronously call the tableColumnsChanged() method of any registered listeners.
| void TableHeaderComponent::removeAllColumns | ( | ) |
Deletes all columns from the table.
If there are any columns to remove, this will asynchronously call the tableColumnsChanged() method of any registered listeners.
| int TableHeaderComponent::getNumColumns | ( | bool | onlyCountVisibleColumns ) | const |
Returns the number of columns in the table.
If onlyCountVisibleColumns is true, this will return the number of visible columns; otherwise it'll return the total number of columns, including hidden ones.
| String TableHeaderComponent::getColumnName | ( | int | columnId ) | const |
Returns the name for a column.
| void TableHeaderComponent::setColumnName | ( | int | columnId, |
| const String & | newName | ||
| ) |
Changes the name of a column.
| void TableHeaderComponent::moveColumn | ( | int | columnId, |
| int | newVisibleIndex | ||
| ) |
Moves a column to a different index in the table.
| columnId | the column to move |
| newVisibleIndex | the target index for it, from 0 to the number of columns currently visible. |
| int TableHeaderComponent::getColumnWidth | ( | int | columnId ) | const |
Returns the width of one of the columns.
| void TableHeaderComponent::setColumnWidth | ( | int | columnId, |
| int | newWidth | ||
| ) |
Changes the width of a column.
This will cause an asynchronous callback to the tableColumnsResized() method of any registered listeners.
| void TableHeaderComponent::setColumnVisible | ( | int | columnId, |
| bool | shouldBeVisible | ||
| ) |
Shows or hides a column.
This can cause an asynchronous callback to the tableColumnsChanged() method of any registered listeners.
| bool TableHeaderComponent::isColumnVisible | ( | int | columnId ) | const |
Returns true if this column is currently visible.
| void TableHeaderComponent::setSortColumnId | ( | int | columnId, |
| bool | sortForwards | ||
| ) |
Changes the column which is the sort column.
This can cause an asynchronous callback to the tableSortOrderChanged() method of any registered listeners.
If this method doesn't actually change the column ID, then no re-sort will take place (you can call reSortTable() to force a re-sort to happen if you've modified the table's contents).
| int TableHeaderComponent::getSortColumnId | ( | ) | const |
Returns the column ID by which the table is currently sorted, or 0 if it is unsorted.
| bool TableHeaderComponent::isSortedForwards | ( | ) | const |
Returns true if the table is currently sorted forwards, or false if it's backwards.
| void TableHeaderComponent::reSortTable | ( | ) |
Triggers a re-sort of the table according to the current sort-column.
If you modifiy the table's contents, you can call this to signal that the table needs to be re-sorted.
(This doesn't do any sorting synchronously - it just asynchronously sends a call to the tableSortOrderChanged() method of any listeners).
| int TableHeaderComponent::getTotalWidth | ( | ) | const |
Returns the total width of all the visible columns in the table.
| int TableHeaderComponent::getIndexOfColumnId | ( | int | columnId, |
| bool | onlyCountVisibleColumns | ||
| ) | const |
Returns the index of a given column.
If there's no such column ID, this will return -1.
If onlyCountVisibleColumns is true, this will return the index amoungst the visible columns; otherwise it'll return the index amongst all the columns, including any hidden ones.
| int TableHeaderComponent::getColumnIdOfIndex | ( | int | index, |
| bool | onlyCountVisibleColumns | ||
| ) | const |
Returns the ID of the column at a given index.
If onlyCountVisibleColumns is true, this will count the index amoungst the visible columns; otherwise it'll count it amongst all the columns, including any hidden ones.
If the index is out-of-range, it'll return 0.
| const Rectangle<int> TableHeaderComponent::getColumnPosition | ( | int | index ) | const |
Returns the rectangle containing of one of the columns.
The index is an index from 0 to the number of columns that are currently visible (hidden ones are not counted). It returns a rectangle showing the position of the column relative to this component's top-left. If the index is out-of-range, an empty rectangle is retrurned.
| int TableHeaderComponent::getColumnIdAtX | ( | int | xToFind ) | const |
Finds the column ID at a given x-position in the component.
If there is a column at this point this returns its ID, or if not, it will return 0.
| void TableHeaderComponent::setStretchToFitActive | ( | bool | shouldStretchToFit ) |
If set to true, this indicates that the columns should be expanded or shrunk to fill the entire width of the component.
By default this is disabled. Turning it on also means that when resizing a column, those on the right will be squashed to fit.
| bool TableHeaderComponent::isStretchToFitActive | ( | ) | const |
Returns true if stretch-to-fit has been enabled.
| void TableHeaderComponent::resizeAllColumnsToFit | ( | int | targetTotalWidth ) |
If stretch-to-fit is enabled, this will resize all the columns to make them fit into the specified width, keeping their relative proportions the same.
If the minimum widths of the columns are too wide to fit into this space, it may actually end up wider.
| void TableHeaderComponent::setPopupMenuActive | ( | bool | hasMenu ) |
Enables or disables the pop-up menu.
The default menu allows the user to show or hide columns. You can add custom items to this menu by overloading the addMenuItems() and reactToMenuItem() methods.
By default the menu is enabled.
| bool TableHeaderComponent::isPopupMenuActive | ( | ) | const |
Returns true if the pop-up menu is enabled.
| String TableHeaderComponent::toString | ( | ) | const |
Returns a string that encapsulates the table's current layout.
This can be restored later using restoreFromString(). It saves the order of the columns, the currently-sorted column, and the widths.
| void TableHeaderComponent::restoreFromString | ( | const String & | storedVersion ) |
Restores the state of the table, based on a string previously created with toString().
| void TableHeaderComponent::addListener | ( | Listener * | newListener ) |
Adds a listener to be informed about things that happen to the header.
| void TableHeaderComponent::removeListener | ( | Listener * | listenerToRemove ) |
Removes a previously-registered listener.
| virtual void TableHeaderComponent::columnClicked | ( | int | columnId, |
| const ModifierKeys & | mods | ||
| ) | [virtual] |
This can be overridden to handle a mouse-click on one of the column headers.
The default implementation will use this click to call getSortColumnId() and change the sort order.
| virtual void TableHeaderComponent::addMenuItems | ( | PopupMenu & | menu, |
| int | columnIdClicked | ||
| ) | [virtual] |
This can be overridden to add custom items to the pop-up menu.
If you override this, you should call the superclass's method to add its column show/hide items, if you want them on the menu as well.
Then to handle the result, override reactToMenuItem().
| virtual void TableHeaderComponent::reactToMenuItem | ( | int | menuReturnId, |
| int | columnIdClicked | ||
| ) | [virtual] |
Override this to handle any custom items that you have added to the pop-up menu with an addMenuItems() override.
If the menuReturnId isn't one of your own custom menu items, you'll need to call TableHeaderComponent::reactToMenuItem() to allow the base class to handle the items that it had added.
| void TableHeaderComponent::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().
| g | the graphics context that must be used to do the drawing operations. |
Reimplemented from Component.
| void TableHeaderComponent::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.
Reimplemented from Component.
| void TableHeaderComponent::mouseMove | ( | const MouseEvent & | e ) | [virtual] |
Called when the mouse moves inside this component.
If the mouse button isn't pressed and the mouse moves over a component, this will be called to let the component react to this.
A component will always get a mouseEnter callback before a mouseMove.
| e | details about the position and status of the mouse event |
Reimplemented from Component.
| void TableHeaderComponent::mouseEnter | ( | const MouseEvent & | e ) | [virtual] |
Called when the mouse first enters this component.
If the mouse button isn't pressed and the mouse moves into a component, this will be called to let the component react to this.
When the mouse button is pressed and held down while being moved in or out of a component, no mouseEnter or mouseExit callbacks are made - only mouseDrag messages are sent to the component that the mouse was originally clicked on, until the button is released.
If you're writing a component that needs to repaint itself when the mouse enters and exits, it might be quicker to use the setRepaintsOnMouseActivity() method.
| e | details about the position and status of the mouse event |
Reimplemented from Component.
| void TableHeaderComponent::mouseExit | ( | const MouseEvent & | e ) | [virtual] |
Called when the mouse moves out of this component.
This will be called when the mouse moves off the edge of this component.
If the mouse button was pressed, and it was then dragged off the edge of the component and released, then this callback will happen when the button is released, after the mouseUp callback.
If you're writing a component that needs to repaint itself when the mouse enters and exits, it might be quicker to use the setRepaintsOnMouseActivity() method.
| e | details about the position and status of the mouse event |
Reimplemented from Component.
| void TableHeaderComponent::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.
| e | details about the position and status of the mouse event |
Reimplemented from Component.
| void TableHeaderComponent::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.
| e | details about the position and status of the mouse event |
Reimplemented from Component.
| void TableHeaderComponent::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.
| e | details about the position and status of the mouse event |
Reimplemented from Component.
| const MouseCursor TableHeaderComponent::getMouseCursor | ( | ) | [virtual] |
Returns the mouse cursor shape to use when the mouse is over this component.
The default implementation will return the cursor that was set by setCursor() but can be overridden for more specialised purposes, e.g. returning different cursors depending on the mouse position.
Reimplemented from Component.
| virtual void TableHeaderComponent::showColumnChooserMenu | ( | int | columnIdClicked ) | [virtual] |
Can be overridden for more control over the pop-up menu behaviour.