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.

577 lines
23KB

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