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.

948 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 rowHeight = owner.getRowHeight();
  170. if (rowHeight > 0)
  171. {
  172. const int y = getViewPositionY();
  173. const int w = getViewedComponent()->getWidth();
  174. const int numNeeded = 2 + getMaximumVisibleHeight() / rowHeight;
  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 / rowHeight;
  183. firstWholeIndex = (y + rowHeight - 1) / rowHeight;
  184. lastWholeIndex = (y + getMaximumVisibleHeight() - 1) / rowHeight;
  185. for (int i = 0; i < numNeeded; ++i)
  186. {
  187. const int row = i + firstIndex;
  188. RowComponent* const rowComp = getComponentForRow (row);
  189. if (rowComp != nullptr)
  190. {
  191. rowComp->setBounds (0, row * rowHeight, w, rowHeight);
  192. rowComp->update (row, owner.isRowSelected (row));
  193. }
  194. }
  195. }
  196. if (owner.headerComponent != nullptr)
  197. owner.headerComponent->setBounds (owner.outlineThickness + getViewedComponent()->getX(),
  198. owner.outlineThickness,
  199. jmax (owner.getWidth() - owner.outlineThickness * 2,
  200. getViewedComponent()->getWidth()),
  201. owner.headerComponent->getHeight());
  202. }
  203. void selectRow (const int row, const int rowHeight, const bool dontScroll,
  204. const int lastRowSelected, const int totalItems, const bool isMouseClick)
  205. {
  206. hasUpdated = false;
  207. if (row < firstWholeIndex && ! dontScroll)
  208. {
  209. setViewPosition (getViewPositionX(), row * rowHeight);
  210. }
  211. else if (row >= lastWholeIndex && ! dontScroll)
  212. {
  213. const int rowsOnScreen = lastWholeIndex - firstWholeIndex;
  214. if (row >= lastRowSelected + rowsOnScreen
  215. && rowsOnScreen < totalItems - 1
  216. && ! isMouseClick)
  217. {
  218. setViewPosition (getViewPositionX(),
  219. jlimit (0, jmax (0, totalItems - rowsOnScreen), row) * rowHeight);
  220. }
  221. else
  222. {
  223. setViewPosition (getViewPositionX(),
  224. jmax (0, (row + 1) * rowHeight - getMaximumVisibleHeight()));
  225. }
  226. }
  227. if (! hasUpdated)
  228. updateContents();
  229. }
  230. void scrollToEnsureRowIsOnscreen (const int row, const int rowHeight)
  231. {
  232. if (row < firstWholeIndex)
  233. {
  234. setViewPosition (getViewPositionX(), row * rowHeight);
  235. }
  236. else if (row >= lastWholeIndex)
  237. {
  238. setViewPosition (getViewPositionX(),
  239. jmax (0, (row + 1) * rowHeight - getMaximumVisibleHeight()));
  240. }
  241. }
  242. void paint (Graphics& g)
  243. {
  244. if (isOpaque())
  245. g.fillAll (owner.findColour (ListBox::backgroundColourId));
  246. }
  247. bool keyPressed (const KeyPress& key)
  248. {
  249. if (key.isKeyCode (KeyPress::upKey)
  250. || key.isKeyCode (KeyPress::downKey)
  251. || key.isKeyCode (KeyPress::pageUpKey)
  252. || key.isKeyCode (KeyPress::pageDownKey)
  253. || key.isKeyCode (KeyPress::homeKey)
  254. || key.isKeyCode (KeyPress::endKey))
  255. {
  256. const int allowableMods = owner.multipleSelection ? ModifierKeys::shiftModifier : 0;
  257. if ((key.getModifiers().getRawFlags() & ~allowableMods) == 0)
  258. {
  259. // we want to avoid these keypresses going to the viewport, and instead allow
  260. // them to pass up to our listbox..
  261. return false;
  262. }
  263. }
  264. return Viewport::keyPressed (key);
  265. }
  266. private:
  267. ListBox& owner;
  268. OwnedArray<RowComponent> rows;
  269. int firstIndex, firstWholeIndex, lastWholeIndex;
  270. bool hasUpdated;
  271. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ListViewport);
  272. };
  273. enum { defaultListRowHeight = 22 };
  274. //==============================================================================
  275. ListBox::ListBox (const String& name, ListBoxModel* const model_)
  276. : Component (name),
  277. model (model_),
  278. totalItems (0),
  279. rowHeight (defaultListRowHeight),
  280. minimumRowWidth (0),
  281. outlineThickness (0),
  282. lastRowSelected (-1),
  283. mouseMoveSelects (false),
  284. multipleSelection (false),
  285. hasDoneInitialUpdate (false)
  286. {
  287. addAndMakeVisible (viewport = new ListViewport (*this));
  288. ListBox::setWantsKeyboardFocus (true);
  289. ListBox::colourChanged();
  290. }
  291. ListBox::~ListBox()
  292. {
  293. headerComponent = nullptr;
  294. viewport = nullptr;
  295. }
  296. void ListBox::setModel (ListBoxModel* const newModel)
  297. {
  298. if (model != newModel)
  299. {
  300. model = newModel;
  301. repaint();
  302. updateContent();
  303. }
  304. }
  305. void ListBox::setMultipleSelectionEnabled (bool b)
  306. {
  307. multipleSelection = b;
  308. }
  309. void ListBox::setMouseMoveSelectsRows (bool b)
  310. {
  311. mouseMoveSelects = b;
  312. if (b)
  313. addMouseListener (this, true);
  314. }
  315. //==============================================================================
  316. void ListBox::paint (Graphics& g)
  317. {
  318. if (! hasDoneInitialUpdate)
  319. updateContent();
  320. g.fillAll (findColour (backgroundColourId));
  321. }
  322. void ListBox::paintOverChildren (Graphics& g)
  323. {
  324. if (outlineThickness > 0)
  325. {
  326. g.setColour (findColour (outlineColourId));
  327. g.drawRect (0, 0, getWidth(), getHeight(), outlineThickness);
  328. }
  329. }
  330. void ListBox::resized()
  331. {
  332. viewport->setBoundsInset (BorderSize<int> (outlineThickness + ((headerComponent != nullptr) ? headerComponent->getHeight() : 0),
  333. outlineThickness, outlineThickness, outlineThickness));
  334. viewport->setSingleStepSizes (20, getRowHeight());
  335. viewport->updateVisibleArea (false);
  336. }
  337. void ListBox::visibilityChanged()
  338. {
  339. viewport->updateVisibleArea (true);
  340. }
  341. Viewport* ListBox::getViewport() const noexcept
  342. {
  343. return viewport;
  344. }
  345. //==============================================================================
  346. void ListBox::updateContent()
  347. {
  348. hasDoneInitialUpdate = true;
  349. totalItems = (model != nullptr) ? model->getNumRows() : 0;
  350. bool selectionChanged = false;
  351. if (selected.size() > 0 && selected [selected.size() - 1] >= totalItems)
  352. {
  353. selected.removeRange (Range <int> (totalItems, std::numeric_limits<int>::max()));
  354. lastRowSelected = getSelectedRow (0);
  355. selectionChanged = true;
  356. }
  357. viewport->updateVisibleArea (isVisible());
  358. viewport->resized();
  359. if (selectionChanged && model != nullptr)
  360. model->selectedRowsChanged (lastRowSelected);
  361. }
  362. //==============================================================================
  363. void ListBox::selectRow (const int row,
  364. bool dontScroll,
  365. bool deselectOthersFirst)
  366. {
  367. selectRowInternal (row, dontScroll, deselectOthersFirst, false);
  368. }
  369. void ListBox::selectRowInternal (const int row,
  370. bool dontScroll,
  371. bool deselectOthersFirst,
  372. bool isMouseClick)
  373. {
  374. if (! multipleSelection)
  375. deselectOthersFirst = true;
  376. if ((! isRowSelected (row))
  377. || (deselectOthersFirst && getNumSelectedRows() > 1))
  378. {
  379. if (isPositiveAndBelow (row, totalItems))
  380. {
  381. if (deselectOthersFirst)
  382. selected.clear();
  383. selected.addRange (Range<int> (row, row + 1));
  384. if (getHeight() == 0 || getWidth() == 0)
  385. dontScroll = true;
  386. viewport->selectRow (row, getRowHeight(), dontScroll,
  387. lastRowSelected, totalItems, isMouseClick);
  388. lastRowSelected = row;
  389. model->selectedRowsChanged (row);
  390. }
  391. else
  392. {
  393. if (deselectOthersFirst)
  394. deselectAllRows();
  395. }
  396. }
  397. }
  398. void ListBox::deselectRow (const int row)
  399. {
  400. if (selected.contains (row))
  401. {
  402. selected.removeRange (Range <int> (row, row + 1));
  403. if (row == lastRowSelected)
  404. lastRowSelected = getSelectedRow (0);
  405. viewport->updateContents();
  406. model->selectedRowsChanged (lastRowSelected);
  407. }
  408. }
  409. void ListBox::setSelectedRows (const SparseSet<int>& setOfRowsToBeSelected,
  410. const bool sendNotificationEventToModel)
  411. {
  412. selected = setOfRowsToBeSelected;
  413. selected.removeRange (Range <int> (totalItems, std::numeric_limits<int>::max()));
  414. if (! isRowSelected (lastRowSelected))
  415. lastRowSelected = getSelectedRow (0);
  416. viewport->updateContents();
  417. if ((model != nullptr) && sendNotificationEventToModel)
  418. model->selectedRowsChanged (lastRowSelected);
  419. }
  420. SparseSet<int> ListBox::getSelectedRows() const
  421. {
  422. return selected;
  423. }
  424. void ListBox::selectRangeOfRows (int firstRow, int lastRow)
  425. {
  426. if (multipleSelection && (firstRow != lastRow))
  427. {
  428. const int numRows = totalItems - 1;
  429. firstRow = jlimit (0, jmax (0, numRows), firstRow);
  430. lastRow = jlimit (0, jmax (0, numRows), lastRow);
  431. selected.addRange (Range <int> (jmin (firstRow, lastRow),
  432. jmax (firstRow, lastRow) + 1));
  433. selected.removeRange (Range <int> (lastRow, lastRow + 1));
  434. }
  435. selectRowInternal (lastRow, false, false, true);
  436. }
  437. void ListBox::flipRowSelection (const int row)
  438. {
  439. if (isRowSelected (row))
  440. deselectRow (row);
  441. else
  442. selectRowInternal (row, false, false, true);
  443. }
  444. void ListBox::deselectAllRows()
  445. {
  446. if (! selected.isEmpty())
  447. {
  448. selected.clear();
  449. lastRowSelected = -1;
  450. viewport->updateContents();
  451. if (model != nullptr)
  452. model->selectedRowsChanged (lastRowSelected);
  453. }
  454. }
  455. void ListBox::selectRowsBasedOnModifierKeys (const int row,
  456. const ModifierKeys& mods,
  457. const bool isMouseUpEvent)
  458. {
  459. if (multipleSelection && mods.isCommandDown())
  460. {
  461. flipRowSelection (row);
  462. }
  463. else if (multipleSelection && mods.isShiftDown() && lastRowSelected >= 0)
  464. {
  465. selectRangeOfRows (lastRowSelected, row);
  466. }
  467. else if ((! mods.isPopupMenu()) || ! isRowSelected (row))
  468. {
  469. selectRowInternal (row, false, ! (multipleSelection && (! isMouseUpEvent) && isRowSelected (row)), true);
  470. }
  471. }
  472. int ListBox::getNumSelectedRows() const
  473. {
  474. return selected.size();
  475. }
  476. int ListBox::getSelectedRow (const int index) const
  477. {
  478. return (isPositiveAndBelow (index, selected.size()))
  479. ? selected [index] : -1;
  480. }
  481. bool ListBox::isRowSelected (const int row) const
  482. {
  483. return selected.contains (row);
  484. }
  485. int ListBox::getLastRowSelected() const
  486. {
  487. return (isRowSelected (lastRowSelected)) ? lastRowSelected : -1;
  488. }
  489. //==============================================================================
  490. int ListBox::getRowContainingPosition (const int x, const int y) const noexcept
  491. {
  492. if (isPositiveAndBelow (x, getWidth()))
  493. {
  494. const int row = (viewport->getViewPositionY() + y - viewport->getY()) / rowHeight;
  495. if (isPositiveAndBelow (row, totalItems))
  496. return row;
  497. }
  498. return -1;
  499. }
  500. int ListBox::getInsertionIndexForPosition (const int x, const int y) const noexcept
  501. {
  502. if (isPositiveAndBelow (x, getWidth()))
  503. {
  504. const int row = (viewport->getViewPositionY() + y + rowHeight / 2 - viewport->getY()) / rowHeight;
  505. return jlimit (0, totalItems, row);
  506. }
  507. return -1;
  508. }
  509. Component* ListBox::getComponentForRowNumber (const int row) const noexcept
  510. {
  511. RowComponent* const listRowComp = viewport->getComponentForRowIfOnscreen (row);
  512. return listRowComp != nullptr ? static_cast <Component*> (listRowComp->customComponent) : 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. int i;
  718. for (i = getNumRowsOnScreen() + 2; --i >= 0;)
  719. {
  720. Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i);
  721. if (rowComp != nullptr && isRowSelected (firstRow + i))
  722. {
  723. const Point<int> pos (getLocalPoint (rowComp, Point<int>()));
  724. const Rectangle<int> rowRect (pos.getX(), pos.getY(), rowComp->getWidth(), rowComp->getHeight());
  725. imageArea = imageArea.getUnion (rowRect);
  726. }
  727. }
  728. imageArea = imageArea.getIntersection (getLocalBounds());
  729. imageX = imageArea.getX();
  730. imageY = imageArea.getY();
  731. Image snapshot (Image::ARGB, imageArea.getWidth(), imageArea.getHeight(), true);
  732. for (i = getNumRowsOnScreen() + 2; --i >= 0;)
  733. {
  734. Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i);
  735. if (rowComp != nullptr && isRowSelected (firstRow + i))
  736. {
  737. const Point<int> pos (getLocalPoint (rowComp, Point<int>()));
  738. Graphics g (snapshot);
  739. g.setOrigin (pos.getX() - imageX, pos.getY() - imageY);
  740. if (g.reduceClipRegion (rowComp->getLocalBounds()))
  741. {
  742. g.beginTransparencyLayer (0.6f);
  743. rowComp->paintEntireComponent (g, false);
  744. g.endTransparencyLayer();
  745. }
  746. }
  747. }
  748. return snapshot;
  749. }
  750. void ListBox::startDragAndDrop (const MouseEvent& e, const var& dragDescription, bool allowDraggingToOtherWindows)
  751. {
  752. DragAndDropContainer* const dragContainer
  753. = DragAndDropContainer::findParentDragContainerFor (this);
  754. if (dragContainer != nullptr)
  755. {
  756. int x, y;
  757. Image dragImage (createSnapshotOfSelectedRows (x, y));
  758. MouseEvent e2 (e.getEventRelativeTo (this));
  759. const Point<int> p (x - e2.x, y - e2.y);
  760. dragContainer->startDragging (dragDescription, this, dragImage, allowDraggingToOtherWindows, &p);
  761. }
  762. else
  763. {
  764. // to be able to do a drag-and-drop operation, the listbox needs to
  765. // be inside a component which is also a DragAndDropContainer.
  766. jassertfalse;
  767. }
  768. }
  769. //==============================================================================
  770. Component* ListBoxModel::refreshComponentForRow (int, bool, Component* existingComponentToUpdate)
  771. {
  772. (void) existingComponentToUpdate;
  773. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code the recycles the components
  774. return nullptr;
  775. }
  776. void ListBoxModel::listBoxItemClicked (int, const MouseEvent&) {}
  777. void ListBoxModel::listBoxItemDoubleClicked (int, const MouseEvent&) {}
  778. void ListBoxModel::backgroundClicked() {}
  779. void ListBoxModel::selectedRowsChanged (int) {}
  780. void ListBoxModel::deleteKeyPressed (int) {}
  781. void ListBoxModel::returnKeyPressed (int) {}
  782. void ListBoxModel::listWasScrolled() {}
  783. var ListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return var::null; }
  784. String ListBoxModel::getTooltipForRow (int) { return String::empty; }