Audio plugin host https://kx.studio/carla
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.

460 lines
20KB

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