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.

958 lines
29KB

  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. class ListBox::RowComponent : public Component,
  18. public TooltipClient
  19. {
  20. public:
  21. RowComponent (ListBox& lb)
  22. : owner (lb), row (-1),
  23. selected (false), isDragging (false), selectRowOnMouseUp (false)
  24. {
  25. }
  26. void paint (Graphics& g) override
  27. {
  28. if (ListBoxModel* m = owner.getModel())
  29. m->paintListBoxItem (row, g, getWidth(), getHeight(), selected);
  30. }
  31. void update (const int newRow, const bool nowSelected)
  32. {
  33. if (row != newRow || selected != nowSelected)
  34. {
  35. repaint();
  36. row = newRow;
  37. selected = nowSelected;
  38. }
  39. if (ListBoxModel* m = owner.getModel())
  40. {
  41. setMouseCursor (m->getMouseCursorForRow (row));
  42. customComponent = m->refreshComponentForRow (newRow, nowSelected, customComponent.release());
  43. if (customComponent != nullptr)
  44. {
  45. addAndMakeVisible (customComponent);
  46. customComponent->setBounds (getLocalBounds());
  47. }
  48. }
  49. }
  50. void mouseDown (const MouseEvent& e) override
  51. {
  52. isDragging = false;
  53. selectRowOnMouseUp = false;
  54. if (isEnabled())
  55. {
  56. if (! selected)
  57. {
  58. owner.selectRowsBasedOnModifierKeys (row, e.mods, false);
  59. if (ListBoxModel* m = owner.getModel())
  60. m->listBoxItemClicked (row, e);
  61. }
  62. else
  63. {
  64. selectRowOnMouseUp = true;
  65. }
  66. }
  67. }
  68. void mouseUp (const MouseEvent& e) override
  69. {
  70. if (isEnabled() && selectRowOnMouseUp && ! isDragging)
  71. {
  72. owner.selectRowsBasedOnModifierKeys (row, e.mods, true);
  73. if (ListBoxModel* m = owner.getModel())
  74. m->listBoxItemClicked (row, e);
  75. }
  76. }
  77. void mouseDoubleClick (const MouseEvent& e) override
  78. {
  79. if (ListBoxModel* m = owner.getModel())
  80. if (isEnabled())
  81. m->listBoxItemDoubleClicked (row, e);
  82. }
  83. void mouseDrag (const MouseEvent& e) override
  84. {
  85. if (ListBoxModel* m = owner.getModel())
  86. {
  87. if (isEnabled() && ! (e.mouseWasClicked() || isDragging))
  88. {
  89. const SparseSet<int> selectedRows (owner.getSelectedRows());
  90. if (selectedRows.size() > 0)
  91. {
  92. const var dragDescription (m->getDragSourceDescription (selectedRows));
  93. if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty())))
  94. {
  95. isDragging = true;
  96. owner.startDragAndDrop (e, dragDescription, true);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. void resized() override
  103. {
  104. if (customComponent != nullptr)
  105. customComponent->setBounds (getLocalBounds());
  106. }
  107. String getTooltip() override
  108. {
  109. if (ListBoxModel* m = owner.getModel())
  110. return m->getTooltipForRow (row);
  111. return String::empty;
  112. }
  113. ScopedPointer<Component> customComponent;
  114. private:
  115. ListBox& owner;
  116. int row;
  117. bool selected, isDragging, selectRowOnMouseUp;
  118. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RowComponent)
  119. };
  120. //==============================================================================
  121. class ListBox::ListViewport : public Viewport
  122. {
  123. public:
  124. ListViewport (ListBox& lb)
  125. : owner (lb)
  126. {
  127. setWantsKeyboardFocus (false);
  128. Component* const content = new Component();
  129. setViewedComponent (content);
  130. content->setWantsKeyboardFocus (false);
  131. }
  132. RowComponent* getComponentForRow (const int row) const noexcept
  133. {
  134. return rows [row % jmax (1, rows.size())];
  135. }
  136. RowComponent* getComponentForRowIfOnscreen (const int row) const noexcept
  137. {
  138. return (row >= firstIndex && row < firstIndex + rows.size())
  139. ? getComponentForRow (row) : nullptr;
  140. }
  141. int getRowNumberOfComponent (Component* const rowComponent) const noexcept
  142. {
  143. const int index = getIndexOfChildComponent (rowComponent);
  144. const int num = rows.size();
  145. for (int i = num; --i >= 0;)
  146. if (((firstIndex + i) % jmax (1, num)) == index)
  147. return firstIndex + i;
  148. return -1;
  149. }
  150. void visibleAreaChanged (const Rectangle<int>&) override
  151. {
  152. updateVisibleArea (true);
  153. if (ListBoxModel* m = owner.getModel())
  154. m->listWasScrolled();
  155. }
  156. void updateVisibleArea (const bool makeSureItUpdatesContent)
  157. {
  158. hasUpdated = false;
  159. const int newX = getViewedComponent()->getX();
  160. int newY = getViewedComponent()->getY();
  161. const int newW = jmax (owner.minimumRowWidth, getMaximumVisibleWidth());
  162. const int newH = owner.totalItems * owner.getRowHeight();
  163. if (newY + newH < getMaximumVisibleHeight() && newH > getMaximumVisibleHeight())
  164. newY = getMaximumVisibleHeight() - newH;
  165. getViewedComponent()->setBounds (newX, newY, newW, newH);
  166. if (makeSureItUpdatesContent && ! hasUpdated)
  167. updateContents();
  168. }
  169. void updateContents()
  170. {
  171. hasUpdated = true;
  172. const int rowH = owner.getRowHeight();
  173. if (rowH > 0)
  174. {
  175. const int y = getViewPositionY();
  176. const int w = getViewedComponent()->getWidth();
  177. const int numNeeded = 2 + getMaximumVisibleHeight() / rowH;
  178. rows.removeRange (numNeeded, rows.size());
  179. while (numNeeded > rows.size())
  180. {
  181. RowComponent* newRow = new RowComponent (owner);
  182. rows.add (newRow);
  183. getViewedComponent()->addAndMakeVisible (newRow);
  184. }
  185. firstIndex = y / rowH;
  186. firstWholeIndex = (y + rowH - 1) / rowH;
  187. lastWholeIndex = (y + getMaximumVisibleHeight() - 1) / rowH;
  188. for (int i = 0; i < numNeeded; ++i)
  189. {
  190. const int row = i + firstIndex;
  191. if (RowComponent* const rowComp = getComponentForRow (row))
  192. {
  193. rowComp->setBounds (0, row * rowH, w, rowH);
  194. rowComp->update (row, owner.isRowSelected (row));
  195. }
  196. }
  197. }
  198. if (owner.headerComponent != nullptr)
  199. owner.headerComponent->setBounds (owner.outlineThickness + getViewedComponent()->getX(),
  200. owner.outlineThickness,
  201. jmax (owner.getWidth() - owner.outlineThickness * 2,
  202. getViewedComponent()->getWidth()),
  203. owner.headerComponent->getHeight());
  204. }
  205. void selectRow (const int row, const int rowH, const bool dontScroll,
  206. const int lastSelectedRow, const int totalRows, const bool isMouseClick)
  207. {
  208. hasUpdated = false;
  209. if (row < firstWholeIndex && ! dontScroll)
  210. {
  211. setViewPosition (getViewPositionX(), row * rowH);
  212. }
  213. else if (row >= lastWholeIndex && ! dontScroll)
  214. {
  215. const int rowsOnScreen = lastWholeIndex - firstWholeIndex;
  216. if (row >= lastSelectedRow + rowsOnScreen
  217. && rowsOnScreen < totalRows - 1
  218. && ! isMouseClick)
  219. {
  220. setViewPosition (getViewPositionX(),
  221. jlimit (0, jmax (0, totalRows - rowsOnScreen), row) * rowH);
  222. }
  223. else
  224. {
  225. setViewPosition (getViewPositionX(),
  226. jmax (0, (row + 1) * rowH - getMaximumVisibleHeight()));
  227. }
  228. }
  229. if (! hasUpdated)
  230. updateContents();
  231. }
  232. void scrollToEnsureRowIsOnscreen (const int row, const int rowH)
  233. {
  234. if (row < firstWholeIndex)
  235. {
  236. setViewPosition (getViewPositionX(), row * rowH);
  237. }
  238. else if (row >= lastWholeIndex)
  239. {
  240. setViewPosition (getViewPositionX(),
  241. jmax (0, (row + 1) * rowH - getMaximumVisibleHeight()));
  242. }
  243. }
  244. void paint (Graphics& g) override
  245. {
  246. if (isOpaque())
  247. g.fillAll (owner.findColour (ListBox::backgroundColourId));
  248. }
  249. bool keyPressed (const KeyPress& key) override
  250. {
  251. if (Viewport::respondsToKey (key))
  252. {
  253. const int allowableMods = owner.multipleSelection ? ModifierKeys::shiftModifier : 0;
  254. if ((key.getModifiers().getRawFlags() & ~allowableMods) == 0)
  255. {
  256. // we want to avoid these keypresses going to the viewport, and instead allow
  257. // them to pass up to our listbox..
  258. return false;
  259. }
  260. }
  261. return Viewport::keyPressed (key);
  262. }
  263. private:
  264. ListBox& owner;
  265. OwnedArray<RowComponent> rows;
  266. int firstIndex, firstWholeIndex, lastWholeIndex;
  267. bool hasUpdated;
  268. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ListViewport)
  269. };
  270. //==============================================================================
  271. class ListBoxMouseMoveSelector : public MouseListener
  272. {
  273. public:
  274. ListBoxMouseMoveSelector (ListBox& lb) : owner (lb)
  275. {
  276. owner.addMouseListener (this, true);
  277. }
  278. void mouseMove (const MouseEvent& e) override
  279. {
  280. const MouseEvent e2 (e.getEventRelativeTo (&owner));
  281. owner.selectRow (owner.getRowContainingPosition (e2.x, e2.y), true);
  282. }
  283. void mouseExit (const MouseEvent& e) override
  284. {
  285. mouseMove (e);
  286. }
  287. private:
  288. ListBox& owner;
  289. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ListBoxMouseMoveSelector)
  290. };
  291. //==============================================================================
  292. ListBox::ListBox (const String& name, ListBoxModel* const m)
  293. : Component (name),
  294. model (m),
  295. totalItems (0),
  296. rowHeight (22),
  297. minimumRowWidth (0),
  298. outlineThickness (0),
  299. lastRowSelected (-1),
  300. multipleSelection (false),
  301. hasDoneInitialUpdate (false)
  302. {
  303. addAndMakeVisible (viewport = new ListViewport (*this));
  304. ListBox::setWantsKeyboardFocus (true);
  305. ListBox::colourChanged();
  306. }
  307. ListBox::~ListBox()
  308. {
  309. headerComponent = nullptr;
  310. viewport = nullptr;
  311. }
  312. void ListBox::setModel (ListBoxModel* const newModel)
  313. {
  314. if (model != newModel)
  315. {
  316. model = newModel;
  317. repaint();
  318. updateContent();
  319. }
  320. }
  321. void ListBox::setMultipleSelectionEnabled (bool b)
  322. {
  323. multipleSelection = b;
  324. }
  325. void ListBox::setMouseMoveSelectsRows (bool b)
  326. {
  327. if (b)
  328. {
  329. if (mouseMoveSelector == nullptr)
  330. mouseMoveSelector = new ListBoxMouseMoveSelector (*this);
  331. }
  332. else
  333. {
  334. mouseMoveSelector = nullptr;
  335. }
  336. }
  337. //==============================================================================
  338. void ListBox::paint (Graphics& g)
  339. {
  340. if (! hasDoneInitialUpdate)
  341. updateContent();
  342. g.fillAll (findColour (backgroundColourId));
  343. }
  344. void ListBox::paintOverChildren (Graphics& g)
  345. {
  346. if (outlineThickness > 0)
  347. {
  348. g.setColour (findColour (outlineColourId));
  349. g.drawRect (getLocalBounds(), outlineThickness);
  350. }
  351. }
  352. void ListBox::resized()
  353. {
  354. viewport->setBoundsInset (BorderSize<int> (outlineThickness + (headerComponent != nullptr ? headerComponent->getHeight() : 0),
  355. outlineThickness, outlineThickness, outlineThickness));
  356. viewport->setSingleStepSizes (20, getRowHeight());
  357. viewport->updateVisibleArea (false);
  358. }
  359. void ListBox::visibilityChanged()
  360. {
  361. viewport->updateVisibleArea (true);
  362. }
  363. Viewport* ListBox::getViewport() const noexcept
  364. {
  365. return viewport;
  366. }
  367. //==============================================================================
  368. void ListBox::updateContent()
  369. {
  370. hasDoneInitialUpdate = true;
  371. totalItems = (model != nullptr) ? model->getNumRows() : 0;
  372. bool selectionChanged = false;
  373. if (selected.size() > 0 && selected [selected.size() - 1] >= totalItems)
  374. {
  375. selected.removeRange (Range <int> (totalItems, std::numeric_limits<int>::max()));
  376. lastRowSelected = getSelectedRow (0);
  377. selectionChanged = true;
  378. }
  379. viewport->updateVisibleArea (isVisible());
  380. viewport->resized();
  381. if (selectionChanged && model != nullptr)
  382. model->selectedRowsChanged (lastRowSelected);
  383. }
  384. //==============================================================================
  385. void ListBox::selectRow (const int row,
  386. bool dontScroll,
  387. bool deselectOthersFirst)
  388. {
  389. selectRowInternal (row, dontScroll, deselectOthersFirst, false);
  390. }
  391. void ListBox::selectRowInternal (const int row,
  392. bool dontScroll,
  393. bool deselectOthersFirst,
  394. bool isMouseClick)
  395. {
  396. if (! multipleSelection)
  397. deselectOthersFirst = true;
  398. if ((! isRowSelected (row))
  399. || (deselectOthersFirst && getNumSelectedRows() > 1))
  400. {
  401. if (isPositiveAndBelow (row, totalItems))
  402. {
  403. if (deselectOthersFirst)
  404. selected.clear();
  405. selected.addRange (Range<int> (row, row + 1));
  406. if (getHeight() == 0 || getWidth() == 0)
  407. dontScroll = true;
  408. viewport->selectRow (row, getRowHeight(), dontScroll,
  409. lastRowSelected, totalItems, isMouseClick);
  410. lastRowSelected = row;
  411. model->selectedRowsChanged (row);
  412. }
  413. else
  414. {
  415. if (deselectOthersFirst)
  416. deselectAllRows();
  417. }
  418. }
  419. }
  420. void ListBox::deselectRow (const int row)
  421. {
  422. if (selected.contains (row))
  423. {
  424. selected.removeRange (Range <int> (row, row + 1));
  425. if (row == lastRowSelected)
  426. lastRowSelected = getSelectedRow (0);
  427. viewport->updateContents();
  428. model->selectedRowsChanged (lastRowSelected);
  429. }
  430. }
  431. void ListBox::setSelectedRows (const SparseSet<int>& setOfRowsToBeSelected,
  432. const NotificationType sendNotificationEventToModel)
  433. {
  434. selected = setOfRowsToBeSelected;
  435. selected.removeRange (Range <int> (totalItems, std::numeric_limits<int>::max()));
  436. if (! isRowSelected (lastRowSelected))
  437. lastRowSelected = getSelectedRow (0);
  438. viewport->updateContents();
  439. if (model != nullptr && sendNotificationEventToModel == sendNotification)
  440. model->selectedRowsChanged (lastRowSelected);
  441. }
  442. SparseSet<int> ListBox::getSelectedRows() const
  443. {
  444. return selected;
  445. }
  446. void ListBox::selectRangeOfRows (int firstRow, int lastRow)
  447. {
  448. if (multipleSelection && (firstRow != lastRow))
  449. {
  450. const int numRows = totalItems - 1;
  451. firstRow = jlimit (0, jmax (0, numRows), firstRow);
  452. lastRow = jlimit (0, jmax (0, numRows), lastRow);
  453. selected.addRange (Range <int> (jmin (firstRow, lastRow),
  454. jmax (firstRow, lastRow) + 1));
  455. selected.removeRange (Range <int> (lastRow, lastRow + 1));
  456. }
  457. selectRowInternal (lastRow, false, false, true);
  458. }
  459. void ListBox::flipRowSelection (const int row)
  460. {
  461. if (isRowSelected (row))
  462. deselectRow (row);
  463. else
  464. selectRowInternal (row, false, false, true);
  465. }
  466. void ListBox::deselectAllRows()
  467. {
  468. if (! selected.isEmpty())
  469. {
  470. selected.clear();
  471. lastRowSelected = -1;
  472. viewport->updateContents();
  473. if (model != nullptr)
  474. model->selectedRowsChanged (lastRowSelected);
  475. }
  476. }
  477. void ListBox::selectRowsBasedOnModifierKeys (const int row,
  478. ModifierKeys mods,
  479. const bool isMouseUpEvent)
  480. {
  481. if (multipleSelection && mods.isCommandDown())
  482. {
  483. flipRowSelection (row);
  484. }
  485. else if (multipleSelection && mods.isShiftDown() && lastRowSelected >= 0)
  486. {
  487. selectRangeOfRows (lastRowSelected, row);
  488. }
  489. else if ((! mods.isPopupMenu()) || ! isRowSelected (row))
  490. {
  491. selectRowInternal (row, false, ! (multipleSelection && (! isMouseUpEvent) && isRowSelected (row)), true);
  492. }
  493. }
  494. int ListBox::getNumSelectedRows() const
  495. {
  496. return selected.size();
  497. }
  498. int ListBox::getSelectedRow (const int index) const
  499. {
  500. return (isPositiveAndBelow (index, selected.size()))
  501. ? selected [index] : -1;
  502. }
  503. bool ListBox::isRowSelected (const int row) const
  504. {
  505. return selected.contains (row);
  506. }
  507. int ListBox::getLastRowSelected() const
  508. {
  509. return isRowSelected (lastRowSelected) ? lastRowSelected : -1;
  510. }
  511. //==============================================================================
  512. int ListBox::getRowContainingPosition (const int x, const int y) const noexcept
  513. {
  514. if (isPositiveAndBelow (x, getWidth()))
  515. {
  516. const int row = (viewport->getViewPositionY() + y - viewport->getY()) / rowHeight;
  517. if (isPositiveAndBelow (row, totalItems))
  518. return row;
  519. }
  520. return -1;
  521. }
  522. int ListBox::getInsertionIndexForPosition (const int x, const int y) const noexcept
  523. {
  524. if (isPositiveAndBelow (x, getWidth()))
  525. {
  526. const int row = (viewport->getViewPositionY() + y + rowHeight / 2 - viewport->getY()) / rowHeight;
  527. return jlimit (0, totalItems, row);
  528. }
  529. return -1;
  530. }
  531. Component* ListBox::getComponentForRowNumber (const int row) const noexcept
  532. {
  533. if (RowComponent* const listRowComp = viewport->getComponentForRowIfOnscreen (row))
  534. return static_cast <Component*> (listRowComp->customComponent);
  535. return nullptr;
  536. }
  537. int ListBox::getRowNumberOfComponent (Component* const rowComponent) const noexcept
  538. {
  539. return viewport->getRowNumberOfComponent (rowComponent);
  540. }
  541. Rectangle<int> ListBox::getRowPosition (const int rowNumber,
  542. const bool relativeToComponentTopLeft) const noexcept
  543. {
  544. int y = viewport->getY() + rowHeight * rowNumber;
  545. if (relativeToComponentTopLeft)
  546. y -= viewport->getViewPositionY();
  547. return Rectangle<int> (viewport->getX(), y,
  548. viewport->getViewedComponent()->getWidth(), rowHeight);
  549. }
  550. void ListBox::setVerticalPosition (const double proportion)
  551. {
  552. const int offscreen = viewport->getViewedComponent()->getHeight() - viewport->getHeight();
  553. viewport->setViewPosition (viewport->getViewPositionX(),
  554. jmax (0, roundToInt (proportion * offscreen)));
  555. }
  556. double ListBox::getVerticalPosition() const
  557. {
  558. const int offscreen = viewport->getViewedComponent()->getHeight() - viewport->getHeight();
  559. return (offscreen > 0) ? viewport->getViewPositionY() / (double) offscreen
  560. : 0;
  561. }
  562. int ListBox::getVisibleRowWidth() const noexcept
  563. {
  564. return viewport->getViewWidth();
  565. }
  566. void ListBox::scrollToEnsureRowIsOnscreen (const int row)
  567. {
  568. viewport->scrollToEnsureRowIsOnscreen (row, getRowHeight());
  569. }
  570. //==============================================================================
  571. bool ListBox::keyPressed (const KeyPress& key)
  572. {
  573. const int numVisibleRows = viewport->getHeight() / getRowHeight();
  574. const bool multiple = multipleSelection
  575. && lastRowSelected >= 0
  576. && key.getModifiers().isShiftDown();
  577. if (key.isKeyCode (KeyPress::upKey))
  578. {
  579. if (multiple)
  580. selectRangeOfRows (lastRowSelected, lastRowSelected - 1);
  581. else
  582. selectRow (jmax (0, lastRowSelected - 1));
  583. }
  584. else if (key.isKeyCode (KeyPress::downKey))
  585. {
  586. if (multiple)
  587. selectRangeOfRows (lastRowSelected, lastRowSelected + 1);
  588. else
  589. selectRow (jmin (totalItems - 1, jmax (0, lastRowSelected) + 1));
  590. }
  591. else if (key.isKeyCode (KeyPress::pageUpKey))
  592. {
  593. if (multiple)
  594. selectRangeOfRows (lastRowSelected, lastRowSelected - numVisibleRows);
  595. else
  596. selectRow (jmax (0, jmax (0, lastRowSelected) - numVisibleRows));
  597. }
  598. else if (key.isKeyCode (KeyPress::pageDownKey))
  599. {
  600. if (multiple)
  601. selectRangeOfRows (lastRowSelected, lastRowSelected + numVisibleRows);
  602. else
  603. selectRow (jmin (totalItems - 1, jmax (0, lastRowSelected) + numVisibleRows));
  604. }
  605. else if (key.isKeyCode (KeyPress::homeKey))
  606. {
  607. if (multiple)
  608. selectRangeOfRows (lastRowSelected, 0);
  609. else
  610. selectRow (0);
  611. }
  612. else if (key.isKeyCode (KeyPress::endKey))
  613. {
  614. if (multiple)
  615. selectRangeOfRows (lastRowSelected, totalItems - 1);
  616. else
  617. selectRow (totalItems - 1);
  618. }
  619. else if (key.isKeyCode (KeyPress::returnKey) && isRowSelected (lastRowSelected))
  620. {
  621. if (model != nullptr)
  622. model->returnKeyPressed (lastRowSelected);
  623. }
  624. else if ((key.isKeyCode (KeyPress::deleteKey) || key.isKeyCode (KeyPress::backspaceKey))
  625. && isRowSelected (lastRowSelected))
  626. {
  627. if (model != nullptr)
  628. model->deleteKeyPressed (lastRowSelected);
  629. }
  630. else if (multipleSelection && key == KeyPress ('a', ModifierKeys::commandModifier, 0))
  631. {
  632. selectRangeOfRows (0, std::numeric_limits<int>::max());
  633. }
  634. else
  635. {
  636. return false;
  637. }
  638. return true;
  639. }
  640. bool ListBox::keyStateChanged (const bool isKeyDown)
  641. {
  642. return isKeyDown
  643. && (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
  644. || KeyPress::isKeyCurrentlyDown (KeyPress::pageUpKey)
  645. || KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
  646. || KeyPress::isKeyCurrentlyDown (KeyPress::pageDownKey)
  647. || KeyPress::isKeyCurrentlyDown (KeyPress::homeKey)
  648. || KeyPress::isKeyCurrentlyDown (KeyPress::endKey)
  649. || KeyPress::isKeyCurrentlyDown (KeyPress::returnKey));
  650. }
  651. void ListBox::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  652. {
  653. bool eventWasUsed = false;
  654. if (wheel.deltaX != 0 && viewport->getHorizontalScrollBar()->isVisible())
  655. {
  656. eventWasUsed = true;
  657. viewport->getHorizontalScrollBar()->mouseWheelMove (e, wheel);
  658. }
  659. if (wheel.deltaY != 0 && viewport->getVerticalScrollBar()->isVisible())
  660. {
  661. eventWasUsed = true;
  662. viewport->getVerticalScrollBar()->mouseWheelMove (e, wheel);
  663. }
  664. if (! eventWasUsed)
  665. Component::mouseWheelMove (e, wheel);
  666. }
  667. void ListBox::mouseUp (const MouseEvent& e)
  668. {
  669. if (e.mouseWasClicked() && model != nullptr)
  670. model->backgroundClicked();
  671. }
  672. //==============================================================================
  673. void ListBox::setRowHeight (const int newHeight)
  674. {
  675. rowHeight = jmax (1, newHeight);
  676. viewport->setSingleStepSizes (20, rowHeight);
  677. updateContent();
  678. }
  679. int ListBox::getNumRowsOnScreen() const noexcept
  680. {
  681. return viewport->getMaximumVisibleHeight() / rowHeight;
  682. }
  683. void ListBox::setMinimumContentWidth (const int newMinimumWidth)
  684. {
  685. minimumRowWidth = newMinimumWidth;
  686. updateContent();
  687. }
  688. int ListBox::getVisibleContentWidth() const noexcept
  689. {
  690. return viewport->getMaximumVisibleWidth();
  691. }
  692. ScrollBar* ListBox::getVerticalScrollBar() const noexcept
  693. {
  694. return viewport->getVerticalScrollBar();
  695. }
  696. ScrollBar* ListBox::getHorizontalScrollBar() const noexcept
  697. {
  698. return viewport->getHorizontalScrollBar();
  699. }
  700. void ListBox::colourChanged()
  701. {
  702. setOpaque (findColour (backgroundColourId).isOpaque());
  703. viewport->setOpaque (isOpaque());
  704. repaint();
  705. }
  706. void ListBox::setOutlineThickness (const int newThickness)
  707. {
  708. outlineThickness = newThickness;
  709. resized();
  710. }
  711. void ListBox::setHeaderComponent (Component* const newHeaderComponent)
  712. {
  713. if (headerComponent != newHeaderComponent)
  714. {
  715. headerComponent = newHeaderComponent;
  716. addAndMakeVisible (newHeaderComponent);
  717. ListBox::resized();
  718. }
  719. }
  720. void ListBox::repaintRow (const int rowNumber) noexcept
  721. {
  722. repaint (getRowPosition (rowNumber, true));
  723. }
  724. Image ListBox::createSnapshotOfSelectedRows (int& imageX, int& imageY)
  725. {
  726. Rectangle<int> imageArea;
  727. const int firstRow = getRowContainingPosition (0, 0);
  728. for (int i = getNumRowsOnScreen() + 2; --i >= 0;)
  729. {
  730. Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i);
  731. if (rowComp != nullptr && isRowSelected (firstRow + i))
  732. {
  733. const Point<int> pos (getLocalPoint (rowComp, Point<int>()));
  734. const Rectangle<int> rowRect (pos.getX(), pos.getY(), rowComp->getWidth(), rowComp->getHeight());
  735. imageArea = imageArea.getUnion (rowRect);
  736. }
  737. }
  738. imageArea = imageArea.getIntersection (getLocalBounds());
  739. imageX = imageArea.getX();
  740. imageY = imageArea.getY();
  741. Image snapshot (Image::ARGB, imageArea.getWidth(), imageArea.getHeight(), true);
  742. for (int i = getNumRowsOnScreen() + 2; --i >= 0;)
  743. {
  744. Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i);
  745. if (rowComp != nullptr && isRowSelected (firstRow + i))
  746. {
  747. Graphics g (snapshot);
  748. g.setOrigin (getLocalPoint (rowComp, Point<int>()) - imageArea.getPosition());
  749. if (g.reduceClipRegion (rowComp->getLocalBounds()))
  750. {
  751. g.beginTransparencyLayer (0.6f);
  752. rowComp->paintEntireComponent (g, false);
  753. g.endTransparencyLayer();
  754. }
  755. }
  756. }
  757. return snapshot;
  758. }
  759. void ListBox::startDragAndDrop (const MouseEvent& e, const var& dragDescription, bool allowDraggingToOtherWindows)
  760. {
  761. if (DragAndDropContainer* const dragContainer = DragAndDropContainer::findParentDragContainerFor (this))
  762. {
  763. int x, y;
  764. Image dragImage (createSnapshotOfSelectedRows (x, y));
  765. MouseEvent e2 (e.getEventRelativeTo (this));
  766. const Point<int> p (x - e2.x, y - e2.y);
  767. dragContainer->startDragging (dragDescription, this, dragImage, allowDraggingToOtherWindows, &p);
  768. }
  769. else
  770. {
  771. // to be able to do a drag-and-drop operation, the listbox needs to
  772. // be inside a component which is also a DragAndDropContainer.
  773. jassertfalse;
  774. }
  775. }
  776. //==============================================================================
  777. Component* ListBoxModel::refreshComponentForRow (int, bool, Component* existingComponentToUpdate)
  778. {
  779. (void) existingComponentToUpdate;
  780. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code that recycles the components
  781. return nullptr;
  782. }
  783. void ListBoxModel::listBoxItemClicked (int, const MouseEvent&) {}
  784. void ListBoxModel::listBoxItemDoubleClicked (int, const MouseEvent&) {}
  785. void ListBoxModel::backgroundClicked() {}
  786. void ListBoxModel::selectedRowsChanged (int) {}
  787. void ListBoxModel::deleteKeyPressed (int) {}
  788. void ListBoxModel::returnKeyPressed (int) {}
  789. void ListBoxModel::listWasScrolled() {}
  790. var ListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return var(); }
  791. String ListBoxModel::getTooltipForRow (int) { return String::empty; }
  792. MouseCursor ListBoxModel::getMouseCursorForRow (int) { return MouseCursor::NormalCursor; }