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.

438 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /**
  20. A component that displays a strip of column headings for a table, and allows these
  21. to be resized, dragged around, etc.
  22. This is just the component that goes at the top of a table. You can use it
  23. directly for custom components, or to create a simple table, use the
  24. TableListBox class.
  25. To use one of these, create it and use addColumn() to add all the columns that you need.
  26. Each column must be given a unique ID number that's used to refer to it.
  27. @see TableListBox, TableHeaderComponent::Listener
  28. */
  29. class JUCE_API TableHeaderComponent : public Component,
  30. private AsyncUpdater
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates an empty table header.
  35. */
  36. TableHeaderComponent();
  37. /** Destructor. */
  38. ~TableHeaderComponent();
  39. //==============================================================================
  40. /** A combination of these flags are passed into the addColumn() method to specify
  41. the properties of a column.
  42. */
  43. enum ColumnPropertyFlags
  44. {
  45. visible = 1, /**< If this is set, the column will be shown; if not, it will be hidden until the user enables it with the pop-up menu. */
  46. resizable = 2, /**< If this is set, the column can be resized by dragging it. */
  47. draggable = 4, /**< If this is set, the column can be dragged around to change its order in the table. */
  48. appearsOnColumnMenu = 8, /**< If this is set, the column will be shown on the pop-up menu allowing it to be hidden/shown. */
  49. sortable = 16, /**< If this is set, then clicking on the column header will set it to be the sort column, and clicking again will reverse the order. */
  50. sortedForwards = 32, /**< If this is set, the column is currently the one by which the table is sorted (forwards). */
  51. sortedBackwards = 64, /**< If this is set, the column is currently the one by which the table is sorted (backwards). */
  52. /** This set of default flags is used as the default parameter value in addColumn(). */
  53. defaultFlags = (visible | resizable | draggable | appearsOnColumnMenu | sortable),
  54. /** A quick way of combining flags for a column that's not resizable. */
  55. notResizable = (visible | draggable | appearsOnColumnMenu | sortable),
  56. /** A quick way of combining flags for a column that's not resizable or sortable. */
  57. notResizableOrSortable = (visible | draggable | appearsOnColumnMenu),
  58. /** A quick way of combining flags for a column that's not sortable. */
  59. notSortable = (visible | resizable | draggable | appearsOnColumnMenu)
  60. };
  61. /** Adds a column to the table.
  62. This will add a column, and asynchronously call the tableColumnsChanged() method of any
  63. registered listeners.
  64. @param columnName the name of the new column. It's ok to have two or more columns with the same name
  65. @param columnId an ID for this column. The ID can be any number apart from 0, but every column must have
  66. a unique ID. This is used to identify the column later on, after the user may have
  67. changed the order that they appear in
  68. @param width the initial width of the column, in pixels
  69. @param maximumWidth a maximum width that the column can take when the user is resizing it. This only applies
  70. if the 'resizable' flag is specified for this column
  71. @param minimumWidth a minimum width that the column can take when the user is resizing it. This only applies
  72. if the 'resizable' flag is specified for this column
  73. @param propertyFlags a combination of some of the values from the ColumnPropertyFlags enum, to define the
  74. properties of this column
  75. @param insertIndex the index at which the column should be added. A value of 0 puts it at the start (left-hand side)
  76. and -1 puts it at the end (right-hand size) of the table. Note that the index the index within
  77. all columns, not just the index amongst those that are currently visible
  78. */
  79. void addColumn (const String& columnName,
  80. int columnId,
  81. int width,
  82. int minimumWidth = 30,
  83. int maximumWidth = -1,
  84. int propertyFlags = defaultFlags,
  85. int insertIndex = -1);
  86. /** Removes a column with the given ID.
  87. If there is such a column, this will asynchronously call the tableColumnsChanged() method of any
  88. registered listeners.
  89. */
  90. void removeColumn (int columnIdToRemove);
  91. /** Deletes all columns from the table.
  92. If there are any columns to remove, this will asynchronously call the tableColumnsChanged() method of any
  93. registered listeners.
  94. */
  95. void removeAllColumns();
  96. /** Returns the number of columns in the table.
  97. If onlyCountVisibleColumns is true, this will return the number of visible columns; otherwise it'll
  98. return the total number of columns, including hidden ones.
  99. @see isColumnVisible
  100. */
  101. int getNumColumns (bool onlyCountVisibleColumns) const;
  102. /** Returns the name for a column.
  103. @see setColumnName
  104. */
  105. String getColumnName (int columnId) const;
  106. /** Changes the name of a column. */
  107. void setColumnName (int columnId, const String& newName);
  108. /** Moves a column to a different index in the table.
  109. @param columnId the column to move
  110. @param newVisibleIndex the target index for it, from 0 to the number of columns currently visible.
  111. */
  112. void moveColumn (int columnId, int newVisibleIndex);
  113. /** Returns the width of one of the columns.
  114. */
  115. int getColumnWidth (int columnId) const;
  116. /** Changes the width of a column.
  117. This will cause an asynchronous callback to the tableColumnsResized() method of any registered listeners.
  118. */
  119. void setColumnWidth (int columnId, int newWidth);
  120. /** Shows or hides a column.
  121. This can cause an asynchronous callback to the tableColumnsChanged() method of any registered listeners.
  122. @see isColumnVisible
  123. */
  124. void setColumnVisible (int columnId, bool shouldBeVisible);
  125. /** Returns true if this column is currently visible.
  126. @see setColumnVisible
  127. */
  128. bool isColumnVisible (int columnId) const;
  129. /** Changes the column which is the sort column.
  130. This can cause an asynchronous callback to the tableSortOrderChanged() method of any registered listeners.
  131. If this method doesn't actually change the column ID, then no re-sort will take place (you can
  132. call reSortTable() to force a re-sort to happen if you've modified the table's contents).
  133. @see getSortColumnId, isSortedForwards, reSortTable
  134. */
  135. void setSortColumnId (int columnId, bool sortForwards);
  136. /** Returns the column ID by which the table is currently sorted, or 0 if it is unsorted.
  137. @see setSortColumnId, isSortedForwards
  138. */
  139. int getSortColumnId() const;
  140. /** Returns true if the table is currently sorted forwards, or false if it's backwards.
  141. @see setSortColumnId
  142. */
  143. bool isSortedForwards() const;
  144. /** Triggers a re-sort of the table according to the current sort-column.
  145. If you modifiy the table's contents, you can call this to signal that the table needs
  146. to be re-sorted.
  147. (This doesn't do any sorting synchronously - it just asynchronously sends a call to the
  148. tableSortOrderChanged() method of any listeners).
  149. */
  150. void reSortTable();
  151. //==============================================================================
  152. /** Returns the total width of all the visible columns in the table.
  153. */
  154. int getTotalWidth() const;
  155. /** Returns the index of a given column.
  156. If there's no such column ID, this will return -1.
  157. If onlyCountVisibleColumns is true, this will return the index amongst the visible columns;
  158. otherwise it'll return the index amongst all the columns, including any hidden ones.
  159. */
  160. int getIndexOfColumnId (int columnId, bool onlyCountVisibleColumns) const;
  161. /** Returns the ID of the column at a given index.
  162. If onlyCountVisibleColumns is true, this will count the index amongst the visible columns;
  163. otherwise it'll count it amongst all the columns, including any hidden ones.
  164. If the index is out-of-range, it'll return 0.
  165. */
  166. int getColumnIdOfIndex (int index, bool onlyCountVisibleColumns) const;
  167. /** Returns the rectangle containing of one of the columns.
  168. The index is an index from 0 to the number of columns that are currently visible (hidden
  169. ones are not counted). It returns a rectangle showing the position of the column relative
  170. to this component's top-left. If the index is out-of-range, an empty rectangle is retrurned.
  171. */
  172. Rectangle<int> getColumnPosition (int index) const;
  173. /** Finds the column ID at a given x-position in the component.
  174. If there is a column at this point this returns its ID, or if not, it will return 0.
  175. */
  176. int getColumnIdAtX (int xToFind) const;
  177. /** If set to true, this indicates that the columns should be expanded or shrunk to fill the
  178. entire width of the component.
  179. By default this is disabled. Turning it on also means that when resizing a column, those
  180. on the right will be squashed to fit.
  181. */
  182. void setStretchToFitActive (bool shouldStretchToFit);
  183. /** Returns true if stretch-to-fit has been enabled.
  184. @see setStretchToFitActive
  185. */
  186. bool isStretchToFitActive() const;
  187. /** If stretch-to-fit is enabled, this will resize all the columns to make them fit into the
  188. specified width, keeping their relative proportions the same.
  189. If the minimum widths of the columns are too wide to fit into this space, it may
  190. actually end up wider.
  191. */
  192. void resizeAllColumnsToFit (int targetTotalWidth);
  193. //==============================================================================
  194. /** Enables or disables the pop-up menu.
  195. The default menu allows the user to show or hide columns. You can add custom
  196. items to this menu by overloading the addMenuItems() and reactToMenuItem() methods.
  197. By default the menu is enabled.
  198. @see isPopupMenuActive, addMenuItems, reactToMenuItem
  199. */
  200. void setPopupMenuActive (bool hasMenu);
  201. /** Returns true if the pop-up menu is enabled.
  202. @see setPopupMenuActive
  203. */
  204. bool isPopupMenuActive() const;
  205. //==============================================================================
  206. /** Returns a string that encapsulates the table's current layout.
  207. This can be restored later using restoreFromString(). It saves the order of
  208. the columns, the currently-sorted column, and the widths.
  209. @see restoreFromString
  210. */
  211. String toString() const;
  212. /** Restores the state of the table, based on a string previously created with
  213. toString().
  214. @see toString
  215. */
  216. void restoreFromString (const String& storedVersion);
  217. //==============================================================================
  218. /**
  219. Receives events from a TableHeaderComponent when columns are resized, moved, etc.
  220. You can register one of these objects for table events using TableHeaderComponent::addListener()
  221. and TableHeaderComponent::removeListener().
  222. @see TableHeaderComponent
  223. */
  224. class JUCE_API Listener
  225. {
  226. public:
  227. //==============================================================================
  228. Listener() {}
  229. /** Destructor. */
  230. virtual ~Listener() {}
  231. //==============================================================================
  232. /** This is called when some of the table's columns are added, removed, hidden,
  233. or rearranged.
  234. */
  235. virtual void tableColumnsChanged (TableHeaderComponent* tableHeader) = 0;
  236. /** This is called when one or more of the table's columns are resized. */
  237. virtual void tableColumnsResized (TableHeaderComponent* tableHeader) = 0;
  238. /** This is called when the column by which the table should be sorted is changed. */
  239. virtual void tableSortOrderChanged (TableHeaderComponent* tableHeader) = 0;
  240. /** This is called when the user begins or ends dragging one of the columns around.
  241. When the user starts dragging a column, this is called with the ID of that
  242. column. When they finish dragging, it is called again with 0 as the ID.
  243. */
  244. virtual void tableColumnDraggingChanged (TableHeaderComponent* tableHeader,
  245. int columnIdNowBeingDragged);
  246. };
  247. /** Adds a listener to be informed about things that happen to the header. */
  248. void addListener (Listener* newListener);
  249. /** Removes a previously-registered listener. */
  250. void removeListener (Listener* listenerToRemove);
  251. //==============================================================================
  252. /** This can be overridden to handle a mouse-click on one of the column headers.
  253. The default implementation will use this click to call getSortColumnId() and
  254. change the sort order.
  255. */
  256. virtual void columnClicked (int columnId, const ModifierKeys& mods);
  257. /** This can be overridden to add custom items to the pop-up menu.
  258. If you override this, you should call the superclass's method to add its
  259. column show/hide items, if you want them on the menu as well.
  260. Then to handle the result, override reactToMenuItem().
  261. @see reactToMenuItem
  262. */
  263. virtual void addMenuItems (PopupMenu& menu, int columnIdClicked);
  264. /** Override this to handle any custom items that you have added to the
  265. pop-up menu with an addMenuItems() override.
  266. If the menuReturnId isn't one of your own custom menu items, you'll need to
  267. call TableHeaderComponent::reactToMenuItem() to allow the base class to
  268. handle the items that it had added.
  269. @see addMenuItems
  270. */
  271. virtual void reactToMenuItem (int menuReturnId, int columnIdClicked);
  272. //==============================================================================
  273. /** This abstract base class is implemented by LookAndFeel classes. */
  274. struct JUCE_API LookAndFeelMethods
  275. {
  276. virtual ~LookAndFeelMethods() {}
  277. virtual void drawTableHeaderBackground (Graphics&, TableHeaderComponent&) = 0;
  278. virtual void drawTableHeaderColumn (Graphics&, const String& columnName, int columnId,
  279. int width, int height,
  280. bool isMouseOver, bool isMouseDown, int columnFlags) = 0;
  281. };
  282. //==============================================================================
  283. /** @internal */
  284. void paint (Graphics&) override;
  285. /** @internal */
  286. void resized() override;
  287. /** @internal */
  288. void mouseMove (const MouseEvent&) override;
  289. /** @internal */
  290. void mouseEnter (const MouseEvent&) override;
  291. /** @internal */
  292. void mouseExit (const MouseEvent&) override;
  293. /** @internal */
  294. void mouseDown (const MouseEvent&) override;
  295. /** @internal */
  296. void mouseDrag (const MouseEvent&) override;
  297. /** @internal */
  298. void mouseUp (const MouseEvent&) override;
  299. /** @internal */
  300. MouseCursor getMouseCursor() override;
  301. /** Can be overridden for more control over the pop-up menu behaviour. */
  302. virtual void showColumnChooserMenu (int columnIdClicked);
  303. private:
  304. struct ColumnInfo
  305. {
  306. String name;
  307. int id, propertyFlags, width, minimumWidth, maximumWidth;
  308. double lastDeliberateWidth;
  309. bool isVisible() const;
  310. };
  311. OwnedArray<ColumnInfo> columns;
  312. Array<Listener*> listeners;
  313. ScopedPointer<Component> dragOverlayComp;
  314. class DragOverlayComp;
  315. bool columnsChanged, columnsResized, sortChanged, menuActive, stretchToFit;
  316. int columnIdBeingResized, columnIdBeingDragged, initialColumnWidth;
  317. int columnIdUnderMouse, draggingColumnOffset, draggingColumnOriginalIndex, lastDeliberateWidth;
  318. ColumnInfo* getInfoForId (int columnId) const;
  319. int visibleIndexToTotalIndex (int visibleIndex) const;
  320. void sendColumnsChanged();
  321. void handleAsyncUpdate() override;
  322. void beginDrag (const MouseEvent&);
  323. void endDrag (int finalIndex);
  324. int getResizeDraggerAt (int mouseX) const;
  325. void updateColumnUnderMouse (const MouseEvent&);
  326. void setColumnUnderMouse (int columnId);
  327. void resizeColumnsToFit (int firstColumnIndex, int targetTotalWidth);
  328. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableHeaderComponent)
  329. };
  330. /** This typedef is just for compatibility with old code - newer code should use the TableHeaderComponent::Listener class directly. */
  331. typedef TableHeaderComponent::Listener TableHeaderListener;