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.

963 lines
29KB

  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. class ListBox::RowComponent : public Component,
  19. public TooltipClient
  20. {
  21. public:
  22. RowComponent (ListBox& lb)
  23. : owner (lb), row (-1),
  24. selected (false), isDragging (false), selectRowOnMouseUp (false)
  25. {
  26. }
  27. void paint (Graphics& g) override
  28. {
  29. if (ListBoxModel* m = owner.getModel())
  30. m->paintListBoxItem (row, g, getWidth(), getHeight(), selected);
  31. }
  32. void update (const int newRow, const bool nowSelected)
  33. {
  34. if (row != newRow || selected != nowSelected)
  35. {
  36. repaint();
  37. row = newRow;
  38. selected = nowSelected;
  39. }
  40. if (ListBoxModel* m = owner.getModel())
  41. {
  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 (key.isKeyCode (KeyPress::upKey)
  252. || key.isKeyCode (KeyPress::downKey)
  253. || key.isKeyCode (KeyPress::pageUpKey)
  254. || key.isKeyCode (KeyPress::pageDownKey)
  255. || key.isKeyCode (KeyPress::homeKey)
  256. || key.isKeyCode (KeyPress::endKey))
  257. {
  258. const int allowableMods = owner.multipleSelection ? ModifierKeys::shiftModifier : 0;
  259. if ((key.getModifiers().getRawFlags() & ~allowableMods) == 0)
  260. {
  261. // we want to avoid these keypresses going to the viewport, and instead allow
  262. // them to pass up to our listbox..
  263. return false;
  264. }
  265. }
  266. return Viewport::keyPressed (key);
  267. }
  268. private:
  269. ListBox& owner;
  270. OwnedArray<RowComponent> rows;
  271. int firstIndex, firstWholeIndex, lastWholeIndex;
  272. bool hasUpdated;
  273. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ListViewport)
  274. };
  275. //==============================================================================
  276. class ListBoxMouseMoveSelector : public MouseListener
  277. {
  278. public:
  279. ListBoxMouseMoveSelector (ListBox& lb) : owner (lb)
  280. {
  281. owner.addMouseListener (this, true);
  282. }
  283. void mouseMove (const MouseEvent& e) override
  284. {
  285. const MouseEvent e2 (e.getEventRelativeTo (&owner));
  286. owner.selectRow (owner.getRowContainingPosition (e2.x, e2.y), true);
  287. }
  288. void mouseExit (const MouseEvent& e) override
  289. {
  290. mouseMove (e);
  291. }
  292. private:
  293. ListBox& owner;
  294. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ListBoxMouseMoveSelector)
  295. };
  296. //==============================================================================
  297. ListBox::ListBox (const String& name, ListBoxModel* const m)
  298. : Component (name),
  299. model (m),
  300. totalItems (0),
  301. rowHeight (22),
  302. minimumRowWidth (0),
  303. outlineThickness (0),
  304. lastRowSelected (-1),
  305. multipleSelection (false),
  306. hasDoneInitialUpdate (false)
  307. {
  308. addAndMakeVisible (viewport = new ListViewport (*this));
  309. ListBox::setWantsKeyboardFocus (true);
  310. ListBox::colourChanged();
  311. }
  312. ListBox::~ListBox()
  313. {
  314. headerComponent = nullptr;
  315. viewport = nullptr;
  316. }
  317. void ListBox::setModel (ListBoxModel* const newModel)
  318. {
  319. if (model != newModel)
  320. {
  321. model = newModel;
  322. repaint();
  323. updateContent();
  324. }
  325. }
  326. void ListBox::setMultipleSelectionEnabled (bool b)
  327. {
  328. multipleSelection = b;
  329. }
  330. void ListBox::setMouseMoveSelectsRows (bool b)
  331. {
  332. if (b)
  333. {
  334. if (mouseMoveSelector == nullptr)
  335. mouseMoveSelector = new ListBoxMouseMoveSelector (*this);
  336. }
  337. else
  338. {
  339. mouseMoveSelector = nullptr;
  340. }
  341. }
  342. //==============================================================================
  343. void ListBox::paint (Graphics& g)
  344. {
  345. if (! hasDoneInitialUpdate)
  346. updateContent();
  347. g.fillAll (findColour (backgroundColourId));
  348. }
  349. void ListBox::paintOverChildren (Graphics& g)
  350. {
  351. if (outlineThickness > 0)
  352. {
  353. g.setColour (findColour (outlineColourId));
  354. g.drawRect (getLocalBounds(), outlineThickness);
  355. }
  356. }
  357. void ListBox::resized()
  358. {
  359. viewport->setBoundsInset (BorderSize<int> (outlineThickness + (headerComponent != nullptr ? headerComponent->getHeight() : 0),
  360. outlineThickness, outlineThickness, outlineThickness));
  361. viewport->setSingleStepSizes (20, getRowHeight());
  362. viewport->updateVisibleArea (false);
  363. }
  364. void ListBox::visibilityChanged()
  365. {
  366. viewport->updateVisibleArea (true);
  367. }
  368. Viewport* ListBox::getViewport() const noexcept
  369. {
  370. return viewport;
  371. }
  372. //==============================================================================
  373. void ListBox::updateContent()
  374. {
  375. hasDoneInitialUpdate = true;
  376. totalItems = (model != nullptr) ? model->getNumRows() : 0;
  377. bool selectionChanged = false;
  378. if (selected.size() > 0 && selected [selected.size() - 1] >= totalItems)
  379. {
  380. selected.removeRange (Range <int> (totalItems, std::numeric_limits<int>::max()));
  381. lastRowSelected = getSelectedRow (0);
  382. selectionChanged = true;
  383. }
  384. viewport->updateVisibleArea (isVisible());
  385. viewport->resized();
  386. if (selectionChanged && model != nullptr)
  387. model->selectedRowsChanged (lastRowSelected);
  388. }
  389. //==============================================================================
  390. void ListBox::selectRow (const int row,
  391. bool dontScroll,
  392. bool deselectOthersFirst)
  393. {
  394. selectRowInternal (row, dontScroll, deselectOthersFirst, false);
  395. }
  396. void ListBox::selectRowInternal (const int row,
  397. bool dontScroll,
  398. bool deselectOthersFirst,
  399. bool isMouseClick)
  400. {
  401. if (! multipleSelection)
  402. deselectOthersFirst = true;
  403. if ((! isRowSelected (row))
  404. || (deselectOthersFirst && getNumSelectedRows() > 1))
  405. {
  406. if (isPositiveAndBelow (row, totalItems))
  407. {
  408. if (deselectOthersFirst)
  409. selected.clear();
  410. selected.addRange (Range<int> (row, row + 1));
  411. if (getHeight() == 0 || getWidth() == 0)
  412. dontScroll = true;
  413. viewport->selectRow (row, getRowHeight(), dontScroll,
  414. lastRowSelected, totalItems, isMouseClick);
  415. lastRowSelected = row;
  416. model->selectedRowsChanged (row);
  417. }
  418. else
  419. {
  420. if (deselectOthersFirst)
  421. deselectAllRows();
  422. }
  423. }
  424. }
  425. void ListBox::deselectRow (const int row)
  426. {
  427. if (selected.contains (row))
  428. {
  429. selected.removeRange (Range <int> (row, row + 1));
  430. if (row == lastRowSelected)
  431. lastRowSelected = getSelectedRow (0);
  432. viewport->updateContents();
  433. model->selectedRowsChanged (lastRowSelected);
  434. }
  435. }
  436. void ListBox::setSelectedRows (const SparseSet<int>& setOfRowsToBeSelected,
  437. const NotificationType sendNotificationEventToModel)
  438. {
  439. selected = setOfRowsToBeSelected;
  440. selected.removeRange (Range <int> (totalItems, std::numeric_limits<int>::max()));
  441. if (! isRowSelected (lastRowSelected))
  442. lastRowSelected = getSelectedRow (0);
  443. viewport->updateContents();
  444. if (model != nullptr && sendNotificationEventToModel == sendNotification)
  445. model->selectedRowsChanged (lastRowSelected);
  446. }
  447. SparseSet<int> ListBox::getSelectedRows() const
  448. {
  449. return selected;
  450. }
  451. void ListBox::selectRangeOfRows (int firstRow, int lastRow)
  452. {
  453. if (multipleSelection && (firstRow != lastRow))
  454. {
  455. const int numRows = totalItems - 1;
  456. firstRow = jlimit (0, jmax (0, numRows), firstRow);
  457. lastRow = jlimit (0, jmax (0, numRows), lastRow);
  458. selected.addRange (Range <int> (jmin (firstRow, lastRow),
  459. jmax (firstRow, lastRow) + 1));
  460. selected.removeRange (Range <int> (lastRow, lastRow + 1));
  461. }
  462. selectRowInternal (lastRow, false, false, true);
  463. }
  464. void ListBox::flipRowSelection (const int row)
  465. {
  466. if (isRowSelected (row))
  467. deselectRow (row);
  468. else
  469. selectRowInternal (row, false, false, true);
  470. }
  471. void ListBox::deselectAllRows()
  472. {
  473. if (! selected.isEmpty())
  474. {
  475. selected.clear();
  476. lastRowSelected = -1;
  477. viewport->updateContents();
  478. if (model != nullptr)
  479. model->selectedRowsChanged (lastRowSelected);
  480. }
  481. }
  482. void ListBox::selectRowsBasedOnModifierKeys (const int row,
  483. ModifierKeys mods,
  484. const bool isMouseUpEvent)
  485. {
  486. if (multipleSelection && mods.isCommandDown())
  487. {
  488. flipRowSelection (row);
  489. }
  490. else if (multipleSelection && mods.isShiftDown() && lastRowSelected >= 0)
  491. {
  492. selectRangeOfRows (lastRowSelected, row);
  493. }
  494. else if ((! mods.isPopupMenu()) || ! isRowSelected (row))
  495. {
  496. selectRowInternal (row, false, ! (multipleSelection && (! isMouseUpEvent) && isRowSelected (row)), true);
  497. }
  498. }
  499. int ListBox::getNumSelectedRows() const
  500. {
  501. return selected.size();
  502. }
  503. int ListBox::getSelectedRow (const int index) const
  504. {
  505. return (isPositiveAndBelow (index, selected.size()))
  506. ? selected [index] : -1;
  507. }
  508. bool ListBox::isRowSelected (const int row) const
  509. {
  510. return selected.contains (row);
  511. }
  512. int ListBox::getLastRowSelected() const
  513. {
  514. return isRowSelected (lastRowSelected) ? lastRowSelected : -1;
  515. }
  516. //==============================================================================
  517. int ListBox::getRowContainingPosition (const int x, const int y) const noexcept
  518. {
  519. if (isPositiveAndBelow (x, getWidth()))
  520. {
  521. const int row = (viewport->getViewPositionY() + y - viewport->getY()) / rowHeight;
  522. if (isPositiveAndBelow (row, totalItems))
  523. return row;
  524. }
  525. return -1;
  526. }
  527. int ListBox::getInsertionIndexForPosition (const int x, const int y) const noexcept
  528. {
  529. if (isPositiveAndBelow (x, getWidth()))
  530. {
  531. const int row = (viewport->getViewPositionY() + y + rowHeight / 2 - viewport->getY()) / rowHeight;
  532. return jlimit (0, totalItems, row);
  533. }
  534. return -1;
  535. }
  536. Component* ListBox::getComponentForRowNumber (const int row) const noexcept
  537. {
  538. if (RowComponent* const listRowComp = viewport->getComponentForRowIfOnscreen (row))
  539. return static_cast <Component*> (listRowComp->customComponent);
  540. return nullptr;
  541. }
  542. int ListBox::getRowNumberOfComponent (Component* const rowComponent) const noexcept
  543. {
  544. return viewport->getRowNumberOfComponent (rowComponent);
  545. }
  546. Rectangle<int> ListBox::getRowPosition (const int rowNumber,
  547. const bool relativeToComponentTopLeft) const noexcept
  548. {
  549. int y = viewport->getY() + rowHeight * rowNumber;
  550. if (relativeToComponentTopLeft)
  551. y -= viewport->getViewPositionY();
  552. return Rectangle<int> (viewport->getX(), y,
  553. viewport->getViewedComponent()->getWidth(), rowHeight);
  554. }
  555. void ListBox::setVerticalPosition (const double proportion)
  556. {
  557. const int offscreen = viewport->getViewedComponent()->getHeight() - viewport->getHeight();
  558. viewport->setViewPosition (viewport->getViewPositionX(),
  559. jmax (0, roundToInt (proportion * offscreen)));
  560. }
  561. double ListBox::getVerticalPosition() const
  562. {
  563. const int offscreen = viewport->getViewedComponent()->getHeight() - viewport->getHeight();
  564. return (offscreen > 0) ? viewport->getViewPositionY() / (double) offscreen
  565. : 0;
  566. }
  567. int ListBox::getVisibleRowWidth() const noexcept
  568. {
  569. return viewport->getViewWidth();
  570. }
  571. void ListBox::scrollToEnsureRowIsOnscreen (const int row)
  572. {
  573. viewport->scrollToEnsureRowIsOnscreen (row, getRowHeight());
  574. }
  575. //==============================================================================
  576. bool ListBox::keyPressed (const KeyPress& key)
  577. {
  578. const int numVisibleRows = viewport->getHeight() / getRowHeight();
  579. const bool multiple = multipleSelection
  580. && lastRowSelected >= 0
  581. && key.getModifiers().isShiftDown();
  582. if (key.isKeyCode (KeyPress::upKey))
  583. {
  584. if (multiple)
  585. selectRangeOfRows (lastRowSelected, lastRowSelected - 1);
  586. else
  587. selectRow (jmax (0, lastRowSelected - 1));
  588. }
  589. else if (key.isKeyCode (KeyPress::downKey))
  590. {
  591. if (multiple)
  592. selectRangeOfRows (lastRowSelected, lastRowSelected + 1);
  593. else
  594. selectRow (jmin (totalItems - 1, jmax (0, lastRowSelected) + 1));
  595. }
  596. else if (key.isKeyCode (KeyPress::pageUpKey))
  597. {
  598. if (multiple)
  599. selectRangeOfRows (lastRowSelected, lastRowSelected - numVisibleRows);
  600. else
  601. selectRow (jmax (0, jmax (0, lastRowSelected) - numVisibleRows));
  602. }
  603. else if (key.isKeyCode (KeyPress::pageDownKey))
  604. {
  605. if (multiple)
  606. selectRangeOfRows (lastRowSelected, lastRowSelected + numVisibleRows);
  607. else
  608. selectRow (jmin (totalItems - 1, jmax (0, lastRowSelected) + numVisibleRows));
  609. }
  610. else if (key.isKeyCode (KeyPress::homeKey))
  611. {
  612. if (multiple)
  613. selectRangeOfRows (lastRowSelected, 0);
  614. else
  615. selectRow (0);
  616. }
  617. else if (key.isKeyCode (KeyPress::endKey))
  618. {
  619. if (multiple)
  620. selectRangeOfRows (lastRowSelected, totalItems - 1);
  621. else
  622. selectRow (totalItems - 1);
  623. }
  624. else if (key.isKeyCode (KeyPress::returnKey) && isRowSelected (lastRowSelected))
  625. {
  626. if (model != nullptr)
  627. model->returnKeyPressed (lastRowSelected);
  628. }
  629. else if ((key.isKeyCode (KeyPress::deleteKey) || key.isKeyCode (KeyPress::backspaceKey))
  630. && isRowSelected (lastRowSelected))
  631. {
  632. if (model != nullptr)
  633. model->deleteKeyPressed (lastRowSelected);
  634. }
  635. else if (multipleSelection && key == KeyPress ('a', ModifierKeys::commandModifier, 0))
  636. {
  637. selectRangeOfRows (0, std::numeric_limits<int>::max());
  638. }
  639. else
  640. {
  641. return false;
  642. }
  643. return true;
  644. }
  645. bool ListBox::keyStateChanged (const bool isKeyDown)
  646. {
  647. return isKeyDown
  648. && (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
  649. || KeyPress::isKeyCurrentlyDown (KeyPress::pageUpKey)
  650. || KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
  651. || KeyPress::isKeyCurrentlyDown (KeyPress::pageDownKey)
  652. || KeyPress::isKeyCurrentlyDown (KeyPress::homeKey)
  653. || KeyPress::isKeyCurrentlyDown (KeyPress::endKey)
  654. || KeyPress::isKeyCurrentlyDown (KeyPress::returnKey));
  655. }
  656. void ListBox::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  657. {
  658. bool eventWasUsed = false;
  659. if (wheel.deltaX != 0 && viewport->getHorizontalScrollBar()->isVisible())
  660. {
  661. eventWasUsed = true;
  662. viewport->getHorizontalScrollBar()->mouseWheelMove (e, wheel);
  663. }
  664. if (wheel.deltaY != 0 && viewport->getVerticalScrollBar()->isVisible())
  665. {
  666. eventWasUsed = true;
  667. viewport->getVerticalScrollBar()->mouseWheelMove (e, wheel);
  668. }
  669. if (! eventWasUsed)
  670. Component::mouseWheelMove (e, wheel);
  671. }
  672. void ListBox::mouseUp (const MouseEvent& e)
  673. {
  674. if (e.mouseWasClicked() && model != nullptr)
  675. model->backgroundClicked();
  676. }
  677. //==============================================================================
  678. void ListBox::setRowHeight (const int newHeight)
  679. {
  680. rowHeight = jmax (1, newHeight);
  681. viewport->setSingleStepSizes (20, rowHeight);
  682. updateContent();
  683. }
  684. int ListBox::getNumRowsOnScreen() const noexcept
  685. {
  686. return viewport->getMaximumVisibleHeight() / rowHeight;
  687. }
  688. void ListBox::setMinimumContentWidth (const int newMinimumWidth)
  689. {
  690. minimumRowWidth = newMinimumWidth;
  691. updateContent();
  692. }
  693. int ListBox::getVisibleContentWidth() const noexcept
  694. {
  695. return viewport->getMaximumVisibleWidth();
  696. }
  697. ScrollBar* ListBox::getVerticalScrollBar() const noexcept
  698. {
  699. return viewport->getVerticalScrollBar();
  700. }
  701. ScrollBar* ListBox::getHorizontalScrollBar() const noexcept
  702. {
  703. return viewport->getHorizontalScrollBar();
  704. }
  705. void ListBox::colourChanged()
  706. {
  707. setOpaque (findColour (backgroundColourId).isOpaque());
  708. viewport->setOpaque (isOpaque());
  709. repaint();
  710. }
  711. void ListBox::setOutlineThickness (const int newThickness)
  712. {
  713. outlineThickness = newThickness;
  714. resized();
  715. }
  716. void ListBox::setHeaderComponent (Component* const newHeaderComponent)
  717. {
  718. if (headerComponent != newHeaderComponent)
  719. {
  720. headerComponent = newHeaderComponent;
  721. addAndMakeVisible (newHeaderComponent);
  722. ListBox::resized();
  723. }
  724. }
  725. void ListBox::repaintRow (const int rowNumber) noexcept
  726. {
  727. repaint (getRowPosition (rowNumber, true));
  728. }
  729. Image ListBox::createSnapshotOfSelectedRows (int& imageX, int& imageY)
  730. {
  731. Rectangle<int> imageArea;
  732. const int firstRow = getRowContainingPosition (0, 0);
  733. for (int i = getNumRowsOnScreen() + 2; --i >= 0;)
  734. {
  735. Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i);
  736. if (rowComp != nullptr && isRowSelected (firstRow + i))
  737. {
  738. const Point<int> pos (getLocalPoint (rowComp, Point<int>()));
  739. const Rectangle<int> rowRect (pos.getX(), pos.getY(), rowComp->getWidth(), rowComp->getHeight());
  740. imageArea = imageArea.getUnion (rowRect);
  741. }
  742. }
  743. imageArea = imageArea.getIntersection (getLocalBounds());
  744. imageX = imageArea.getX();
  745. imageY = imageArea.getY();
  746. Image snapshot (Image::ARGB, imageArea.getWidth(), imageArea.getHeight(), true);
  747. for (int i = getNumRowsOnScreen() + 2; --i >= 0;)
  748. {
  749. Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i);
  750. if (rowComp != nullptr && isRowSelected (firstRow + i))
  751. {
  752. const Point<int> pos (getLocalPoint (rowComp, Point<int>()));
  753. Graphics g (snapshot);
  754. g.setOrigin (pos.getX() - imageX, pos.getY() - imageY);
  755. if (g.reduceClipRegion (rowComp->getLocalBounds()))
  756. {
  757. g.beginTransparencyLayer (0.6f);
  758. rowComp->paintEntireComponent (g, false);
  759. g.endTransparencyLayer();
  760. }
  761. }
  762. }
  763. return snapshot;
  764. }
  765. void ListBox::startDragAndDrop (const MouseEvent& e, const var& dragDescription, bool allowDraggingToOtherWindows)
  766. {
  767. if (DragAndDropContainer* const dragContainer = DragAndDropContainer::findParentDragContainerFor (this))
  768. {
  769. int x, y;
  770. Image dragImage (createSnapshotOfSelectedRows (x, y));
  771. MouseEvent e2 (e.getEventRelativeTo (this));
  772. const Point<int> p (x - e2.x, y - e2.y);
  773. dragContainer->startDragging (dragDescription, this, dragImage, allowDraggingToOtherWindows, &p);
  774. }
  775. else
  776. {
  777. // to be able to do a drag-and-drop operation, the listbox needs to
  778. // be inside a component which is also a DragAndDropContainer.
  779. jassertfalse;
  780. }
  781. }
  782. //==============================================================================
  783. Component* ListBoxModel::refreshComponentForRow (int, bool, Component* existingComponentToUpdate)
  784. {
  785. (void) existingComponentToUpdate;
  786. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code that recycles the components
  787. return nullptr;
  788. }
  789. void ListBoxModel::listBoxItemClicked (int, const MouseEvent&) {}
  790. void ListBoxModel::listBoxItemDoubleClicked (int, const MouseEvent&) {}
  791. void ListBoxModel::backgroundClicked() {}
  792. void ListBoxModel::selectedRowsChanged (int) {}
  793. void ListBoxModel::deleteKeyPressed (int) {}
  794. void ListBoxModel::returnKeyPressed (int) {}
  795. void ListBoxModel::listWasScrolled() {}
  796. var ListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return var::null; }
  797. String ListBoxModel::getTooltipForRow (int) { return String::empty; }