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.

341 lines
13KB

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