The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_TABLELISTBOX_JUCEHEADER__
  19. #define __JUCE_TABLELISTBOX_JUCEHEADER__
  20. #include "juce_TableHeaderComponent.h"
  21. #include "juce_ListBox.h"
  22. //==============================================================================
  23. /**
  24. One of these is used by a TableListBox as the data model for the table's contents.
  25. The virtual methods that you override in this class take care of drawing the
  26. table cells, and reacting to events.
  27. @see TableListBox
  28. */
  29. class JUCE_API TableListBoxModel
  30. {
  31. public:
  32. //==============================================================================
  33. TableListBoxModel() {}
  34. /** Destructor. */
  35. virtual ~TableListBoxModel() {}
  36. //==============================================================================
  37. /** This must return the number of rows currently in the table.
  38. If the number of rows changes, you must call TableListBox::updateContent() to
  39. cause it to refresh the list.
  40. */
  41. virtual int getNumRows() = 0;
  42. /** This must draw the background behind one of the rows in the table.
  43. The graphics context has its origin at the row's top-left, and your method
  44. should fill the area specified by the width and height parameters.
  45. */
  46. virtual void paintRowBackground (Graphics& g,
  47. int rowNumber,
  48. int width, int height,
  49. bool rowIsSelected) = 0;
  50. /** This must draw one of the cells.
  51. The graphics context's origin will already be set to the top-left of the cell,
  52. whose size is specified by (width, height).
  53. */
  54. virtual void paintCell (Graphics& g,
  55. int rowNumber,
  56. int columnId,
  57. int width, int height,
  58. bool rowIsSelected) = 0;
  59. //==============================================================================
  60. /** This is used to create or update a custom component to go in a cell.
  61. Any cell may contain a custom component, or can just be drawn with the paintCell() method
  62. and handle mouse clicks with cellClicked().
  63. This method will be called whenever a custom component might need to be updated - e.g.
  64. when the table is changed, or TableListBox::updateContent() is called.
  65. If you don't need a custom component for the specified cell, then return nullptr.
  66. (Bear in mind that even if you're not creating a new component, you may still need to
  67. delete existingComponentToUpdate if it's non-null).
  68. If you do want a custom component, and the existingComponentToUpdate is null, then
  69. this method must create a new component suitable for the cell, and return it.
  70. If the existingComponentToUpdate is non-null, it will be a pointer to a component previously created
  71. by this method. In this case, the method must either update it to make sure it's correctly representing
  72. the given cell (which may be different from the one that the component was created for), or it can
  73. delete this component and return a new one.
  74. */
  75. virtual Component* refreshComponentForCell (int rowNumber, int columnId, bool isRowSelected,
  76. Component* existingComponentToUpdate);
  77. //==============================================================================
  78. /** This callback is made when the user clicks on one of the cells in the table.
  79. The mouse event's coordinates will be relative to the entire table row.
  80. @see cellDoubleClicked, backgroundClicked
  81. */
  82. virtual void cellClicked (int rowNumber, int columnId, const MouseEvent& e);
  83. /** This callback is made when the user clicks on one of the cells in the table.
  84. The mouse event's coordinates will be relative to the entire table row.
  85. @see cellClicked, backgroundClicked
  86. */
  87. virtual void cellDoubleClicked (int rowNumber, int columnId, const MouseEvent& e);
  88. /** This can be overridden to react to the user double-clicking on a part of the list where
  89. there are no rows.
  90. @see cellClicked
  91. */
  92. virtual void backgroundClicked();
  93. //==============================================================================
  94. /** This callback is made when the table's sort order is changed.
  95. This could be because the user has clicked a column header, or because the
  96. TableHeaderComponent::setSortColumnId() method was called.
  97. If you implement this, your method should re-sort the table using the given
  98. column as the key.
  99. */
  100. virtual void sortOrderChanged (int newSortColumnId, bool isForwards);
  101. //==============================================================================
  102. /** Returns the best width for one of the columns.
  103. If you implement this method, you should measure the width of all the items
  104. in this column, and return the best size.
  105. Returning 0 means that the column shouldn't be changed.
  106. This is used by TableListBox::autoSizeColumn() and TableListBox::autoSizeAllColumns().
  107. */
  108. virtual int getColumnAutoSizeWidth (int columnId);
  109. /** Returns a tooltip for a particular cell in the table.
  110. */
  111. virtual String getCellTooltip (int rowNumber, int columnId);
  112. //==============================================================================
  113. /** Override this to be informed when rows are selected or deselected.
  114. @see ListBox::selectedRowsChanged()
  115. */
  116. virtual void selectedRowsChanged (int lastRowSelected);
  117. /** Override this to be informed when the delete key is pressed.
  118. @see ListBox::deleteKeyPressed()
  119. */
  120. virtual void deleteKeyPressed (int lastRowSelected);
  121. /** Override this to be informed when the return key is pressed.
  122. @see ListBox::returnKeyPressed()
  123. */
  124. virtual void returnKeyPressed (int lastRowSelected);
  125. /** Override this to be informed when the list is scrolled.
  126. This might be caused by the user moving the scrollbar, or by programmatic changes
  127. to the list position.
  128. */
  129. virtual void listWasScrolled();
  130. /** To allow rows from your table to be dragged-and-dropped, implement this method.
  131. If this returns a non-null variant then when the user drags a row, the table will try to
  132. find a DragAndDropContainer in its parent hierarchy, and will use it to trigger a
  133. drag-and-drop operation, using this string as the source description, and the listbox
  134. itself as the source component.
  135. @see getDragSourceCustomData, DragAndDropContainer::startDragging
  136. */
  137. virtual var getDragSourceDescription (const SparseSet<int>& currentlySelectedRows);
  138. };
  139. //==============================================================================
  140. /**
  141. A table of cells, using a TableHeaderComponent as its header.
  142. This component makes it easy to create a table by providing a TableListBoxModel as
  143. the data source.
  144. @see TableListBoxModel, TableHeaderComponent
  145. */
  146. class JUCE_API TableListBox : public ListBox,
  147. private ListBoxModel,
  148. private TableHeaderComponent::Listener
  149. {
  150. public:
  151. //==============================================================================
  152. /** Creates a TableListBox.
  153. The model pointer passed-in can be null, in which case you can set it later
  154. with setModel().
  155. */
  156. TableListBox (const String& componentName = String::empty,
  157. TableListBoxModel* model = 0);
  158. /** Destructor. */
  159. ~TableListBox();
  160. //==============================================================================
  161. /** Changes the TableListBoxModel that is being used for this table.
  162. */
  163. void setModel (TableListBoxModel* newModel);
  164. /** Returns the model currently in use. */
  165. TableListBoxModel* getModel() const { return model; }
  166. //==============================================================================
  167. /** Returns the header component being used in this table. */
  168. TableHeaderComponent& getHeader() const { return *header; }
  169. /** Sets the header component to use for the table.
  170. The table will take ownership of the component that you pass in, and will delete it
  171. when it's no longer needed.
  172. */
  173. void setHeader (TableHeaderComponent* newHeader);
  174. /** Changes the height of the table header component.
  175. @see getHeaderHeight
  176. */
  177. void setHeaderHeight (int newHeight);
  178. /** Returns the height of the table header.
  179. @see setHeaderHeight
  180. */
  181. int getHeaderHeight() const;
  182. //==============================================================================
  183. /** Resizes a column to fit its contents.
  184. This uses TableListBoxModel::getColumnAutoSizeWidth() to find the best width,
  185. and applies that to the column.
  186. @see autoSizeAllColumns, TableHeaderComponent::setColumnWidth
  187. */
  188. void autoSizeColumn (int columnId);
  189. /** Calls autoSizeColumn() for all columns in the table. */
  190. void autoSizeAllColumns();
  191. /** Enables or disables the auto size options on the popup menu.
  192. By default, these are enabled.
  193. */
  194. void setAutoSizeMenuOptionShown (bool shouldBeShown);
  195. /** True if the auto-size options should be shown on the menu.
  196. @see setAutoSizeMenuOptionsShown
  197. */
  198. bool isAutoSizeMenuOptionShown() const;
  199. /** Returns the position of one of the cells in the table.
  200. If relativeToComponentTopLeft is true, the co-ordinates are relative to
  201. the table component's top-left. The row number isn't checked to see if it's
  202. in-range, but the column ID must exist or this will return an empty rectangle.
  203. If relativeToComponentTopLeft is false, the co-ords are relative to the
  204. top-left of the table's top-left cell.
  205. */
  206. Rectangle<int> getCellPosition (int columnId, int rowNumber,
  207. bool relativeToComponentTopLeft) const;
  208. /** Returns the component that currently represents a given cell.
  209. If the component for this cell is off-screen or if the position is out-of-range,
  210. this may return 0.
  211. @see getCellPosition
  212. */
  213. Component* getCellComponent (int columnId, int rowNumber) const;
  214. /** Scrolls horizontally if necessary to make sure that a particular column is visible.
  215. @see ListBox::scrollToEnsureRowIsOnscreen
  216. */
  217. void scrollToEnsureColumnIsOnscreen (int columnId);
  218. //==============================================================================
  219. /** @internal */
  220. int getNumRows();
  221. /** @internal */
  222. void paintListBoxItem (int, Graphics&, int, int, bool);
  223. /** @internal */
  224. Component* refreshComponentForRow (int rowNumber, bool isRowSelected, Component* existingComponentToUpdate);
  225. /** @internal */
  226. void selectedRowsChanged (int lastRowSelected);
  227. /** @internal */
  228. void deleteKeyPressed (int currentSelectedRow);
  229. /** @internal */
  230. void returnKeyPressed (int currentSelectedRow);
  231. /** @internal */
  232. void backgroundClicked();
  233. /** @internal */
  234. void listWasScrolled();
  235. /** @internal */
  236. void tableColumnsChanged (TableHeaderComponent*);
  237. /** @internal */
  238. void tableColumnsResized (TableHeaderComponent*);
  239. /** @internal */
  240. void tableSortOrderChanged (TableHeaderComponent*);
  241. /** @internal */
  242. void tableColumnDraggingChanged (TableHeaderComponent*, int);
  243. /** @internal */
  244. void resized();
  245. private:
  246. //==============================================================================
  247. class Header;
  248. class RowComp;
  249. TableHeaderComponent* header;
  250. TableListBoxModel* model;
  251. int columnIdNowBeingDragged;
  252. bool autoSizeOptionsShown;
  253. void updateColumnComponents() const;
  254. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableListBox);
  255. };
  256. #endif // __JUCE_TABLELISTBOX_JUCEHEADER__