Add remaining files
[juce-lv2.git] / juce / source / src / gui / components / controls / juce_TableListBox.h
blob19bfd202f4d9a049459615592801ebf91f8c1ba5
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_TABLELISTBOX_JUCEHEADER__
27 #define __JUCE_TABLELISTBOX_JUCEHEADER__
29 #include "juce_TableHeaderComponent.h"
30 #include "juce_ListBox.h"
33 //==============================================================================
34 /**
35 One of these is used by a TableListBox as the data model for the table's contents.
37 The virtual methods that you override in this class take care of drawing the
38 table cells, and reacting to events.
40 @see TableListBox
42 class JUCE_API TableListBoxModel
44 public:
45 //==============================================================================
46 TableListBoxModel() {}
48 /** Destructor. */
49 virtual ~TableListBoxModel() {}
51 //==============================================================================
52 /** This must return the number of rows currently in the table.
54 If the number of rows changes, you must call TableListBox::updateContent() to
55 cause it to refresh the list.
57 virtual int getNumRows() = 0;
59 /** This must draw the background behind one of the rows in the table.
61 The graphics context has its origin at the row's top-left, and your method
62 should fill the area specified by the width and height parameters.
64 virtual void paintRowBackground (Graphics& g,
65 int rowNumber,
66 int width, int height,
67 bool rowIsSelected) = 0;
69 /** This must draw one of the cells.
71 The graphics context's origin will already be set to the top-left of the cell,
72 whose size is specified by (width, height).
74 virtual void paintCell (Graphics& g,
75 int rowNumber,
76 int columnId,
77 int width, int height,
78 bool rowIsSelected) = 0;
80 //==============================================================================
81 /** This is used to create or update a custom component to go in a cell.
83 Any cell may contain a custom component, or can just be drawn with the paintCell() method
84 and handle mouse clicks with cellClicked().
86 This method will be called whenever a custom component might need to be updated - e.g.
87 when the table is changed, or TableListBox::updateContent() is called.
89 If you don't need a custom component for the specified cell, then return 0.
91 If you do want a custom component, and the existingComponentToUpdate is null, then
92 this method must create a new component suitable for the cell, and return it.
94 If the existingComponentToUpdate is non-null, it will be a pointer to a component previously created
95 by this method. In this case, the method must either update it to make sure it's correctly representing
96 the given cell (which may be different from the one that the component was created for), or it can
97 delete this component and return a new one.
99 virtual Component* refreshComponentForCell (int rowNumber, int columnId, bool isRowSelected,
100 Component* existingComponentToUpdate);
102 //==============================================================================
103 /** This callback is made when the user clicks on one of the cells in the table.
105 The mouse event's coordinates will be relative to the entire table row.
106 @see cellDoubleClicked, backgroundClicked
108 virtual void cellClicked (int rowNumber, int columnId, const MouseEvent& e);
110 /** This callback is made when the user clicks on one of the cells in the table.
112 The mouse event's coordinates will be relative to the entire table row.
113 @see cellClicked, backgroundClicked
115 virtual void cellDoubleClicked (int rowNumber, int columnId, const MouseEvent& e);
117 /** This can be overridden to react to the user double-clicking on a part of the list where
118 there are no rows.
120 @see cellClicked
122 virtual void backgroundClicked();
124 //==============================================================================
125 /** This callback is made when the table's sort order is changed.
127 This could be because the user has clicked a column header, or because the
128 TableHeaderComponent::setSortColumnId() method was called.
130 If you implement this, your method should re-sort the table using the given
131 column as the key.
133 virtual void sortOrderChanged (int newSortColumnId, bool isForwards);
135 //==============================================================================
136 /** Returns the best width for one of the columns.
138 If you implement this method, you should measure the width of all the items
139 in this column, and return the best size.
141 Returning 0 means that the column shouldn't be changed.
143 This is used by TableListBox::autoSizeColumn() and TableListBox::autoSizeAllColumns().
145 virtual int getColumnAutoSizeWidth (int columnId);
147 /** Returns a tooltip for a particular cell in the table.
149 virtual const String getCellTooltip (int rowNumber, int columnId);
151 //==============================================================================
152 /** Override this to be informed when rows are selected or deselected.
154 @see ListBox::selectedRowsChanged()
156 virtual void selectedRowsChanged (int lastRowSelected);
158 /** Override this to be informed when the delete key is pressed.
160 @see ListBox::deleteKeyPressed()
162 virtual void deleteKeyPressed (int lastRowSelected);
164 /** Override this to be informed when the return key is pressed.
166 @see ListBox::returnKeyPressed()
168 virtual void returnKeyPressed (int lastRowSelected);
170 /** Override this to be informed when the list is scrolled.
172 This might be caused by the user moving the scrollbar, or by programmatic changes
173 to the list position.
175 virtual void listWasScrolled();
177 /** To allow rows from your table to be dragged-and-dropped, implement this method.
179 If this returns a non-null variant then when the user drags a row, the table will try to
180 find a DragAndDropContainer in its parent hierarchy, and will use it to trigger a
181 drag-and-drop operation, using this string as the source description, and the listbox
182 itself as the source component.
184 @see getDragSourceCustomData, DragAndDropContainer::startDragging
186 virtual const var getDragSourceDescription (const SparseSet<int>& currentlySelectedRows);
190 //==============================================================================
192 A table of cells, using a TableHeaderComponent as its header.
194 This component makes it easy to create a table by providing a TableListBoxModel as
195 the data source.
198 @see TableListBoxModel, TableHeaderComponent
200 class JUCE_API TableListBox : public ListBox,
201 private ListBoxModel,
202 private TableHeaderComponent::Listener
204 public:
205 //==============================================================================
206 /** Creates a TableListBox.
208 The model pointer passed-in can be null, in which case you can set it later
209 with setModel().
211 TableListBox (const String& componentName = String::empty,
212 TableListBoxModel* model = 0);
214 /** Destructor. */
215 ~TableListBox();
217 //==============================================================================
218 /** Changes the TableListBoxModel that is being used for this table.
220 void setModel (TableListBoxModel* newModel);
222 /** Returns the model currently in use. */
223 TableListBoxModel* getModel() const { return model; }
225 //==============================================================================
226 /** Returns the header component being used in this table. */
227 TableHeaderComponent& getHeader() const { return *header; }
229 /** Sets the header component to use for the table.
230 The table will take ownership of the component that you pass in, and will delete it
231 when it's no longer needed.
233 void setHeader (TableHeaderComponent* newHeader);
235 /** Changes the height of the table header component.
236 @see getHeaderHeight
238 void setHeaderHeight (int newHeight);
240 /** Returns the height of the table header.
241 @see setHeaderHeight
243 int getHeaderHeight() const;
245 //==============================================================================
246 /** Resizes a column to fit its contents.
248 This uses TableListBoxModel::getColumnAutoSizeWidth() to find the best width,
249 and applies that to the column.
251 @see autoSizeAllColumns, TableHeaderComponent::setColumnWidth
253 void autoSizeColumn (int columnId);
255 /** Calls autoSizeColumn() for all columns in the table. */
256 void autoSizeAllColumns();
258 /** Enables or disables the auto size options on the popup menu.
260 By default, these are enabled.
262 void setAutoSizeMenuOptionShown (bool shouldBeShown);
264 /** True if the auto-size options should be shown on the menu.
265 @see setAutoSizeMenuOptionsShown
267 bool isAutoSizeMenuOptionShown() const;
269 /** Returns the position of one of the cells in the table.
271 If relativeToComponentTopLeft is true, the co-ordinates are relative to
272 the table component's top-left. The row number isn't checked to see if it's
273 in-range, but the column ID must exist or this will return an empty rectangle.
275 If relativeToComponentTopLeft is false, the co-ords are relative to the
276 top-left of the table's top-left cell.
278 const Rectangle<int> getCellPosition (int columnId, int rowNumber,
279 bool relativeToComponentTopLeft) const;
281 /** Returns the component that currently represents a given cell.
282 If the component for this cell is off-screen or if the position is out-of-range,
283 this may return 0.
284 @see getCellPosition
286 Component* getCellComponent (int columnId, int rowNumber) const;
288 /** Scrolls horizontally if necessary to make sure that a particular column is visible.
290 @see ListBox::scrollToEnsureRowIsOnscreen
292 void scrollToEnsureColumnIsOnscreen (int columnId);
294 //==============================================================================
295 /** @internal */
296 int getNumRows();
297 /** @internal */
298 void paintListBoxItem (int, Graphics&, int, int, bool);
299 /** @internal */
300 Component* refreshComponentForRow (int rowNumber, bool isRowSelected, Component* existingComponentToUpdate);
301 /** @internal */
302 void selectedRowsChanged (int lastRowSelected);
303 /** @internal */
304 void deleteKeyPressed (int currentSelectedRow);
305 /** @internal */
306 void returnKeyPressed (int currentSelectedRow);
307 /** @internal */
308 void backgroundClicked();
309 /** @internal */
310 void listWasScrolled();
311 /** @internal */
312 void tableColumnsChanged (TableHeaderComponent*);
313 /** @internal */
314 void tableColumnsResized (TableHeaderComponent*);
315 /** @internal */
316 void tableSortOrderChanged (TableHeaderComponent*);
317 /** @internal */
318 void tableColumnDraggingChanged (TableHeaderComponent*, int);
319 /** @internal */
320 void resized();
323 private:
324 //==============================================================================
325 TableHeaderComponent* header;
326 TableListBoxModel* model;
327 int columnIdNowBeingDragged;
328 bool autoSizeOptionsShown;
330 void updateColumnComponents() const;
332 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableListBox);
336 #endif // __JUCE_TABLELISTBOX_JUCEHEADER__