juce_ListBox.h 25 KB

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