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.

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