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.

565 lines
23KB

  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_LISTBOX_H_INCLUDED
  18. #define JUCE_LISTBOX_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A subclass of this is used to drive a ListBox.
  22. @see ListBox
  23. */
  24. class JUCE_API ListBoxModel
  25. {
  26. public:
  27. //==============================================================================
  28. /** Destructor. */
  29. virtual ~ListBoxModel() {}
  30. //==============================================================================
  31. /** This has to return the number of items in the list.
  32. @see ListBox::getNumRows()
  33. */
  34. virtual int getNumRows() = 0;
  35. /** This method must be implemented to draw a row of the list. */
  36. virtual void paintListBoxItem (int rowNumber,
  37. Graphics& g,
  38. int width, int height,
  39. bool rowIsSelected) = 0;
  40. /** This is used to create or update a custom component to go in a row of the list.
  41. Any row may contain a custom component, or can just be drawn with the paintListBoxItem() method
  42. and handle mouse clicks with listBoxItemClicked().
  43. This method will be called whenever a custom component might need to be updated - e.g.
  44. when the table is changed, or TableListBox::updateContent() is called.
  45. If you don't need a custom component for the specified row, then return nullptr.
  46. (Bear in mind that even if you're not creating a new component, you may still need to
  47. delete existingComponentToUpdate if it's non-null).
  48. If you do want a custom component, and the existingComponentToUpdate is null, then
  49. this method must create a suitable new component and return it.
  50. If the existingComponentToUpdate is non-null, it will be a pointer to a component previously created
  51. by this method. In this case, the method must either update it to make sure it's correctly representing
  52. the given row (which may be different from the one that the component was created for), or it can
  53. delete this component and return a new one.
  54. The component that your method returns will be deleted by the ListBox when it is no longer needed.
  55. Bear in mind that if you put a custom component inside the row but still want the
  56. listbox to automatically handle clicking, selection, etc, then you'll need to make sure
  57. your custom component doesn't intercept all the mouse events that land on it, e.g by
  58. using Component::setInterceptsMouseClicks().
  59. */
  60. virtual Component* refreshComponentForRow (int rowNumber, bool isRowSelected,
  61. Component* existingComponentToUpdate);
  62. /** This can be overridden to react to the user clicking on a row.
  63. @see listBoxItemDoubleClicked
  64. */
  65. virtual void listBoxItemClicked (int row, const MouseEvent& e);
  66. /** This can be overridden to react to the user double-clicking on a row.
  67. @see listBoxItemClicked
  68. */
  69. virtual void listBoxItemDoubleClicked (int row, const MouseEvent& e);
  70. /** This can be overridden to react to the user clicking on a part of the list where
  71. there are no rows.
  72. @see listBoxItemClicked
  73. */
  74. virtual void backgroundClicked();
  75. /** Override this to be informed when rows are selected or deselected.
  76. This will be called whenever a row is selected or deselected. If a range of
  77. rows is selected all at once, this will just be called once for that event.
  78. @param lastRowSelected the last row that the user selected. If no
  79. rows are currently selected, this may be -1.
  80. */
  81. virtual void selectedRowsChanged (int lastRowSelected);
  82. /** Override this to be informed when the delete key is pressed.
  83. If no rows are selected when they press the key, this won't be called.
  84. @param lastRowSelected the last row that had been selected when they pressed the
  85. key - if there are multiple selections, this might not be
  86. very useful
  87. */
  88. virtual void deleteKeyPressed (int lastRowSelected);
  89. /** Override this to be informed when the return key is pressed.
  90. If no rows are selected when they press the key, this won't be called.
  91. @param lastRowSelected the last row that had been selected when they pressed the
  92. key - if there are multiple selections, this might not be
  93. very useful
  94. */
  95. virtual void returnKeyPressed (int lastRowSelected);
  96. /** Override this to be informed when the list is scrolled.
  97. This might be caused by the user moving the scrollbar, or by programmatic changes
  98. to the list position.
  99. */
  100. virtual void listWasScrolled();
  101. /** To allow rows from your list to be dragged-and-dropped, implement this method.
  102. If this returns a non-null variant then when the user drags a row, the listbox will
  103. try to find a DragAndDropContainer in its parent hierarchy, and will use it to trigger
  104. a drag-and-drop operation, using this string as the source description, with the listbox
  105. itself as the source component.
  106. @see DragAndDropContainer::startDragging
  107. */
  108. virtual var getDragSourceDescription (const SparseSet<int>& currentlySelectedRows);
  109. /** You can override this to provide tool tips for specific rows.
  110. @see TooltipClient
  111. */
  112. virtual String getTooltipForRow (int row);
  113. };
  114. //==============================================================================
  115. /**
  116. A list of items that can be scrolled vertically.
  117. To create a list, you'll need to create a subclass of ListBoxModel. This can
  118. either paint each row of the list and respond to events via callbacks, or for
  119. more specialised tasks, it can supply a custom component to fill each row.
  120. @see ComboBox, TableListBox
  121. */
  122. class JUCE_API ListBox : public Component,
  123. public SettableTooltipClient
  124. {
  125. public:
  126. //==============================================================================
  127. /** Creates a ListBox.
  128. The model pointer passed-in can be null, in which case you can set it later
  129. with setModel().
  130. */
  131. ListBox (const String& componentName = String::empty,
  132. ListBoxModel* model = nullptr);
  133. /** Destructor. */
  134. ~ListBox();
  135. //==============================================================================
  136. /** Changes the current data model to display. */
  137. void setModel (ListBoxModel* newModel);
  138. /** Returns the current list model. */
  139. ListBoxModel* getModel() const noexcept { return model; }
  140. //==============================================================================
  141. /** Causes the list to refresh its content.
  142. Call this when the number of rows in the list changes, or if you want it
  143. to call refreshComponentForRow() on all the row components.
  144. This must only be called from the main message thread.
  145. */
  146. void updateContent();
  147. //==============================================================================
  148. /** Turns on multiple-selection of rows.
  149. By default this is disabled.
  150. When your row component gets clicked you'll need to call the
  151. selectRowsBasedOnModifierKeys() method to tell the list that it's been
  152. clicked and to get it to do the appropriate selection based on whether
  153. the ctrl/shift keys are held down.
  154. */
  155. void setMultipleSelectionEnabled (bool shouldBeEnabled);
  156. /** Makes the list react to mouse moves by selecting the row that the mouse if over.
  157. This function is here primarily for the ComboBox class to use, but might be
  158. useful for some other purpose too.
  159. */
  160. void setMouseMoveSelectsRows (bool shouldSelect);
  161. //==============================================================================
  162. /** Selects a row.
  163. If the row is already selected, this won't do anything.
  164. @param rowNumber the row to select
  165. @param dontScrollToShowThisRow if true, the list's position won't change; if false and
  166. the selected row is off-screen, it'll scroll to make
  167. sure that row is on-screen
  168. @param deselectOthersFirst if true and there are multiple selections, these will
  169. first be deselected before this item is selected
  170. @see isRowSelected, selectRowsBasedOnModifierKeys, flipRowSelection, deselectRow,
  171. deselectAllRows, selectRangeOfRows
  172. */
  173. void selectRow (int rowNumber,
  174. bool dontScrollToShowThisRow = false,
  175. bool deselectOthersFirst = true);
  176. /** Selects a set of rows.
  177. This will add these rows to the current selection, so you might need to
  178. clear the current selection first with deselectAllRows()
  179. @param firstRow the first row to select (inclusive)
  180. @param lastRow the last row to select (inclusive)
  181. */
  182. void selectRangeOfRows (int firstRow,
  183. int lastRow);
  184. /** Deselects a row.
  185. If it's not currently selected, this will do nothing.
  186. @see selectRow, deselectAllRows
  187. */
  188. void deselectRow (int rowNumber);
  189. /** Deselects any currently selected rows.
  190. @see deselectRow
  191. */
  192. void deselectAllRows();
  193. /** Selects or deselects a row.
  194. If the row's currently selected, this deselects it, and vice-versa.
  195. */
  196. void flipRowSelection (int rowNumber);
  197. /** Returns a sparse set indicating the rows that are currently selected.
  198. @see setSelectedRows
  199. */
  200. SparseSet<int> getSelectedRows() const;
  201. /** Sets the rows that should be selected, based on an explicit set of ranges.
  202. If sendNotificationEventToModel is true, the ListBoxModel::selectedRowsChanged()
  203. method will be called. If it's false, no notification will be sent to the model.
  204. @see getSelectedRows
  205. */
  206. void setSelectedRows (const SparseSet<int>& setOfRowsToBeSelected,
  207. NotificationType sendNotificationEventToModel = sendNotification);
  208. /** Checks whether a row is selected.
  209. */
  210. bool isRowSelected (int rowNumber) const;
  211. /** Returns the number of rows that are currently selected.
  212. @see getSelectedRow, isRowSelected, getLastRowSelected
  213. */
  214. int getNumSelectedRows() const;
  215. /** Returns the row number of a selected row.
  216. This will return the row number of the Nth selected row. The row numbers returned will
  217. be sorted in order from low to high.
  218. @param index the index of the selected row to return, (from 0 to getNumSelectedRows() - 1)
  219. @returns the row number, or -1 if the index was out of range or if there aren't any rows
  220. selected
  221. @see getNumSelectedRows, isRowSelected, getLastRowSelected
  222. */
  223. int getSelectedRow (int index = 0) const;
  224. /** Returns the last row that the user selected.
  225. This isn't the same as the highest row number that is currently selected - if the user
  226. had multiply-selected rows 10, 5 and then 6 in that order, this would return 6.
  227. If nothing is selected, it will return -1.
  228. */
  229. int getLastRowSelected() const;
  230. /** Multiply-selects rows based on the modifier keys.
  231. If no modifier keys are down, this will select the given row and
  232. deselect any others.
  233. If the ctrl (or command on the Mac) key is down, it'll flip the
  234. state of the selected row.
  235. If the shift key is down, it'll select up to the given row from the
  236. last row selected.
  237. @see selectRow
  238. */
  239. void selectRowsBasedOnModifierKeys (int rowThatWasClickedOn,
  240. ModifierKeys modifiers,
  241. bool isMouseUpEvent);
  242. //==============================================================================
  243. /** Scrolls the list to a particular position.
  244. The proportion is between 0 and 1.0, so 0 scrolls to the top of the list,
  245. 1.0 scrolls to the bottom.
  246. If the total number of rows all fit onto the screen at once, then this
  247. method won't do anything.
  248. @see getVerticalPosition
  249. */
  250. void setVerticalPosition (double newProportion);
  251. /** Returns the current vertical position as a proportion of the total.
  252. This can be used in conjunction with setVerticalPosition() to save and restore
  253. the list's position. It returns a value in the range 0 to 1.
  254. @see setVerticalPosition
  255. */
  256. double getVerticalPosition() const;
  257. /** Scrolls if necessary to make sure that a particular row is visible. */
  258. void scrollToEnsureRowIsOnscreen (int row);
  259. /** Returns a pointer to the vertical scrollbar. */
  260. ScrollBar* getVerticalScrollBar() const noexcept;
  261. /** Returns a pointer to the horizontal scrollbar. */
  262. ScrollBar* getHorizontalScrollBar() const noexcept;
  263. /** Finds the row index that contains a given x,y position.
  264. The position is relative to the ListBox's top-left.
  265. If no row exists at this position, the method will return -1.
  266. @see getComponentForRowNumber
  267. */
  268. int getRowContainingPosition (int x, int y) const noexcept;
  269. /** Finds a row index that would be the most suitable place to insert a new
  270. item for a given position.
  271. This is useful when the user is e.g. dragging and dropping onto the listbox,
  272. because it lets you easily choose the best position to insert the item that
  273. they drop, based on where they drop it.
  274. If the position is out of range, this will return -1. If the position is
  275. beyond the end of the list, it will return getNumRows() to indicate the end
  276. of the list.
  277. @see getComponentForRowNumber
  278. */
  279. int getInsertionIndexForPosition (int x, int y) const noexcept;
  280. /** Returns the position of one of the rows, relative to the top-left of
  281. the listbox.
  282. This may be off-screen, and the range of the row number that is passed-in is
  283. not checked to see if it's a valid row.
  284. */
  285. Rectangle<int> getRowPosition (int rowNumber,
  286. bool relativeToComponentTopLeft) const noexcept;
  287. /** Finds the row component for a given row in the list.
  288. The component returned will have been created using createRowComponent().
  289. If the component for this row is off-screen or if the row is out-of-range,
  290. this will return 0.
  291. @see getRowContainingPosition
  292. */
  293. Component* getComponentForRowNumber (int rowNumber) const noexcept;
  294. /** Returns the row number that the given component represents.
  295. If the component isn't one of the list's rows, this will return -1.
  296. */
  297. int getRowNumberOfComponent (Component* rowComponent) const noexcept;
  298. /** Returns the width of a row (which may be less than the width of this component
  299. if there's a scrollbar).
  300. */
  301. int getVisibleRowWidth() const noexcept;
  302. //==============================================================================
  303. /** Sets the height of each row in the list.
  304. The default height is 22 pixels.
  305. @see getRowHeight
  306. */
  307. void setRowHeight (int newHeight);
  308. /** Returns the height of a row in the list.
  309. @see setRowHeight
  310. */
  311. int getRowHeight() const noexcept { return rowHeight; }
  312. /** Returns the number of rows actually visible.
  313. This is the number of whole rows which will fit on-screen, so the value might
  314. be more than the actual number of rows in the list.
  315. */
  316. int getNumRowsOnScreen() const noexcept;
  317. //==============================================================================
  318. /** A set of colour IDs to use to change the colour of various aspects of the label.
  319. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  320. methods.
  321. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  322. */
  323. enum ColourIds
  324. {
  325. backgroundColourId = 0x1002800, /**< The background colour to fill the list with.
  326. Make this transparent if you don't want the background to be filled. */
  327. outlineColourId = 0x1002810, /**< An optional colour to use to draw a border around the list.
  328. Make this transparent to not have an outline. */
  329. textColourId = 0x1002820 /**< The preferred colour to use for drawing text in the listbox. */
  330. };
  331. /** Sets the thickness of a border that will be drawn around the box.
  332. To set the colour of the outline, use @code setColour (ListBox::outlineColourId, colourXYZ); @endcode
  333. @see outlineColourId
  334. */
  335. void setOutlineThickness (int outlineThickness);
  336. /** Returns the thickness of outline that will be drawn around the listbox.
  337. @see setOutlineColour
  338. */
  339. int getOutlineThickness() const noexcept { return outlineThickness; }
  340. /** Sets a component that the list should use as a header.
  341. This will position the given component at the top of the list, maintaining the
  342. height of the component passed-in, but rescaling it horizontally to match the
  343. width of the items in the listbox.
  344. The component will be deleted when setHeaderComponent() is called with a
  345. different component, or when the listbox is deleted.
  346. */
  347. void setHeaderComponent (Component* newHeaderComponent);
  348. /** Changes the width of the rows in the list.
  349. This can be used to make the list's row components wider than the list itself - the
  350. width of the rows will be either the width of the list or this value, whichever is
  351. greater, and if the rows become wider than the list, a horizontal scrollbar will
  352. appear.
  353. The default value for this is 0, which means that the rows will always
  354. be the same width as the list.
  355. */
  356. void setMinimumContentWidth (int newMinimumWidth);
  357. /** Returns the space currently available for the row items, taking into account
  358. borders, scrollbars, etc.
  359. */
  360. int getVisibleContentWidth() const noexcept;
  361. /** Repaints one of the rows.
  362. This does not invoke updateContent(), it just invokes a straightforward repaint
  363. for the area covered by this row.
  364. */
  365. void repaintRow (int rowNumber) noexcept;
  366. /** This fairly obscure method creates an image that just shows the currently
  367. selected row components.
  368. It's a handy method for doing drag-and-drop, as it can be passed to the
  369. DragAndDropContainer for use as the drag image.
  370. Note that it will make the row components temporarily invisible, so if you're
  371. using custom components this could affect them if they're sensitive to that
  372. sort of thing.
  373. @see Component::createComponentSnapshot
  374. */
  375. virtual Image createSnapshotOfSelectedRows (int& x, int& y);
  376. /** Returns the viewport that this ListBox uses.
  377. You may need to use this to change parameters such as whether scrollbars
  378. are shown, etc.
  379. */
  380. Viewport* getViewport() const noexcept;
  381. //==============================================================================
  382. /** @internal */
  383. bool keyPressed (const KeyPress&) override;
  384. /** @internal */
  385. bool keyStateChanged (bool isKeyDown) override;
  386. /** @internal */
  387. void paint (Graphics&) override;
  388. /** @internal */
  389. void paintOverChildren (Graphics&) override;
  390. /** @internal */
  391. void resized() override;
  392. /** @internal */
  393. void visibilityChanged() override;
  394. /** @internal */
  395. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  396. /** @internal */
  397. void mouseUp (const MouseEvent&) override;
  398. /** @internal */
  399. void colourChanged() override;
  400. /** @internal */
  401. void startDragAndDrop (const MouseEvent&, const var& dragDescription, bool allowDraggingToOtherWindows);
  402. private:
  403. //==============================================================================
  404. JUCE_PUBLIC_IN_DLL_BUILD (class ListViewport)
  405. JUCE_PUBLIC_IN_DLL_BUILD (class RowComponent)
  406. friend class ListViewport;
  407. friend class TableListBox;
  408. ListBoxModel* model;
  409. ScopedPointer<ListViewport> viewport;
  410. ScopedPointer<Component> headerComponent;
  411. ScopedPointer<MouseListener> mouseMoveSelector;
  412. int totalItems, rowHeight, minimumRowWidth;
  413. int outlineThickness;
  414. int lastRowSelected;
  415. bool multipleSelection, hasDoneInitialUpdate;
  416. SparseSet<int> selected;
  417. void selectRowInternal (int rowNumber, bool dontScrollToShowThisRow,
  418. bool deselectOthersFirst, bool isMouseClick);
  419. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  420. // This method's bool parameter has changed: see the new method signature.
  421. JUCE_DEPRECATED (void setSelectedRows (const SparseSet<int>&, bool));
  422. #endif
  423. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ListBox)
  424. };
  425. #endif // JUCE_LISTBOX_H_INCLUDED