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.

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