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.

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