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.

juce_TableHeaderComponent.h 19KB

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