juce_TableListBox.h 13 KB

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