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.

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