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.

619 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. class TableListBox::RowComp : public Component,
  21. public TooltipClient
  22. {
  23. public:
  24. RowComp (TableListBox& tlb) noexcept
  25. : owner (tlb)
  26. {
  27. setFocusContainerType (FocusContainerType::focusContainer);
  28. }
  29. void paint (Graphics& g) override
  30. {
  31. if (auto* tableModel = owner.getModel())
  32. {
  33. tableModel->paintRowBackground (g, row, getWidth(), getHeight(), isSelected);
  34. auto& headerComp = owner.getHeader();
  35. auto numColumns = headerComp.getNumColumns (true);
  36. auto clipBounds = g.getClipBounds();
  37. for (int i = 0; i < numColumns; ++i)
  38. {
  39. if (columnComponents[i] == nullptr)
  40. {
  41. auto columnRect = headerComp.getColumnPosition (i).withHeight (getHeight());
  42. if (columnRect.getX() >= clipBounds.getRight())
  43. break;
  44. if (columnRect.getRight() > clipBounds.getX())
  45. {
  46. Graphics::ScopedSaveState ss (g);
  47. if (g.reduceClipRegion (columnRect))
  48. {
  49. g.setOrigin (columnRect.getX(), 0);
  50. tableModel->paintCell (g, row, headerComp.getColumnIdOfIndex (i, true),
  51. columnRect.getWidth(), columnRect.getHeight(), isSelected);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. void update (int newRow, bool isNowSelected)
  59. {
  60. jassert (newRow >= 0);
  61. if (newRow != row || isNowSelected != isSelected)
  62. {
  63. row = newRow;
  64. isSelected = isNowSelected;
  65. repaint();
  66. }
  67. auto* tableModel = owner.getModel();
  68. if (tableModel != nullptr && row < owner.getNumRows())
  69. {
  70. const Identifier columnProperty ("_tableColumnId");
  71. auto numColumns = owner.getHeader().getNumColumns (true);
  72. for (int i = 0; i < numColumns; ++i)
  73. {
  74. auto columnId = owner.getHeader().getColumnIdOfIndex (i, true);
  75. auto* comp = columnComponents[i];
  76. if (comp != nullptr && columnId != static_cast<int> (comp->getProperties() [columnProperty]))
  77. {
  78. columnComponents.set (i, nullptr);
  79. comp = nullptr;
  80. }
  81. comp = tableModel->refreshComponentForCell (row, columnId, isSelected, comp);
  82. columnComponents.set (i, comp, false);
  83. if (comp != nullptr)
  84. {
  85. comp->getProperties().set (columnProperty, columnId);
  86. addAndMakeVisible (comp);
  87. resizeCustomComp (i);
  88. }
  89. }
  90. columnComponents.removeRange (numColumns, columnComponents.size());
  91. }
  92. else
  93. {
  94. columnComponents.clear();
  95. }
  96. }
  97. void resized() override
  98. {
  99. for (int i = columnComponents.size(); --i >= 0;)
  100. resizeCustomComp (i);
  101. }
  102. void resizeCustomComp (int index)
  103. {
  104. if (auto* c = columnComponents.getUnchecked (index))
  105. c->setBounds (owner.getHeader().getColumnPosition (index)
  106. .withY (0).withHeight (getHeight()));
  107. }
  108. void mouseDown (const MouseEvent& e) override
  109. {
  110. isDragging = false;
  111. selectRowOnMouseUp = false;
  112. if (isEnabled())
  113. {
  114. if (! isSelected)
  115. {
  116. owner.selectRowsBasedOnModifierKeys (row, e.mods, false);
  117. auto columnId = owner.getHeader().getColumnIdAtX (e.x);
  118. if (columnId != 0)
  119. if (auto* m = owner.getModel())
  120. m->cellClicked (row, columnId, e);
  121. }
  122. else
  123. {
  124. selectRowOnMouseUp = true;
  125. }
  126. }
  127. }
  128. void mouseDrag (const MouseEvent& e) override
  129. {
  130. if (isEnabled()
  131. && owner.getModel() != nullptr
  132. && e.mouseWasDraggedSinceMouseDown()
  133. && ! isDragging)
  134. {
  135. SparseSet<int> rowsToDrag;
  136. if (owner.selectOnMouseDown || owner.isRowSelected (row))
  137. rowsToDrag = owner.getSelectedRows();
  138. else
  139. rowsToDrag.addRange (Range<int>::withStartAndLength (row, 1));
  140. if (rowsToDrag.size() > 0)
  141. {
  142. auto dragDescription = owner.getModel()->getDragSourceDescription (rowsToDrag);
  143. if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty())))
  144. {
  145. isDragging = true;
  146. owner.startDragAndDrop (e, rowsToDrag, dragDescription, true);
  147. }
  148. }
  149. }
  150. }
  151. void mouseUp (const MouseEvent& e) override
  152. {
  153. if (selectRowOnMouseUp && e.mouseWasClicked() && isEnabled())
  154. {
  155. owner.selectRowsBasedOnModifierKeys (row, e.mods, true);
  156. auto columnId = owner.getHeader().getColumnIdAtX (e.x);
  157. if (columnId != 0)
  158. if (TableListBoxModel* m = owner.getModel())
  159. m->cellClicked (row, columnId, e);
  160. }
  161. }
  162. void mouseDoubleClick (const MouseEvent& e) override
  163. {
  164. auto columnId = owner.getHeader().getColumnIdAtX (e.x);
  165. if (columnId != 0)
  166. if (auto* m = owner.getModel())
  167. m->cellDoubleClicked (row, columnId, e);
  168. }
  169. String getTooltip() override
  170. {
  171. auto columnId = owner.getHeader().getColumnIdAtX (getMouseXYRelative().getX());
  172. if (columnId != 0)
  173. if (auto* m = owner.getModel())
  174. return m->getCellTooltip (row, columnId);
  175. return {};
  176. }
  177. Component* findChildComponentForColumn (int columnId) const
  178. {
  179. return columnComponents [owner.getHeader().getIndexOfColumnId (columnId, true)];
  180. }
  181. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
  182. {
  183. return std::make_unique<RowAccessibilityHandler> (*this);
  184. }
  185. //==============================================================================
  186. class RowAccessibilityHandler : public AccessibilityHandler
  187. {
  188. public:
  189. RowAccessibilityHandler (RowComp& rowComp)
  190. : AccessibilityHandler (rowComp,
  191. AccessibilityRole::row,
  192. getListRowAccessibilityActions (rowComp),
  193. { std::make_unique<RowComponentCellInterface> (*this) }),
  194. rowComponent (rowComp)
  195. {
  196. }
  197. String getTitle() const override
  198. {
  199. if (auto* m = rowComponent.owner.ListBox::model)
  200. return m->getNameForRow (rowComponent.row);
  201. return {};
  202. }
  203. String getHelp() const override { return rowComponent.getTooltip(); }
  204. AccessibleState getCurrentState() const override
  205. {
  206. if (auto* m = rowComponent.owner.getModel())
  207. if (rowComponent.row >= m->getNumRows())
  208. return AccessibleState().withIgnored();
  209. auto state = AccessibilityHandler::getCurrentState();
  210. if (rowComponent.owner.multipleSelection)
  211. state = state.withMultiSelectable();
  212. else
  213. state = state.withSelectable();
  214. if (rowComponent.isSelected)
  215. return state.withSelected();
  216. return state;
  217. }
  218. class RowComponentCellInterface : public AccessibilityCellInterface
  219. {
  220. public:
  221. RowComponentCellInterface (RowAccessibilityHandler& handler)
  222. : owner (handler)
  223. {
  224. }
  225. int getColumnIndex() const override { return 0; }
  226. int getColumnSpan() const override { return 1; }
  227. int getRowIndex() const override { return owner.rowComponent.row; }
  228. int getRowSpan() const override { return 1; }
  229. int getDisclosureLevel() const override { return 0; }
  230. const AccessibilityHandler* getTableHandler() const override { return owner.rowComponent.owner.getAccessibilityHandler(); }
  231. private:
  232. RowAccessibilityHandler& owner;
  233. };
  234. private:
  235. RowComp& rowComponent;
  236. };
  237. //==============================================================================
  238. TableListBox& owner;
  239. OwnedArray<Component> columnComponents;
  240. int row = -1;
  241. bool isSelected = false, isDragging = false, selectRowOnMouseUp = false;
  242. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RowComp)
  243. };
  244. //==============================================================================
  245. class TableListBox::Header : public TableHeaderComponent
  246. {
  247. public:
  248. Header (TableListBox& tlb) : owner (tlb) {}
  249. void addMenuItems (PopupMenu& menu, int columnIdClicked)
  250. {
  251. if (owner.isAutoSizeMenuOptionShown())
  252. {
  253. menu.addItem (autoSizeColumnId, TRANS("Auto-size this column"), columnIdClicked != 0);
  254. menu.addItem (autoSizeAllId, TRANS("Auto-size all columns"), owner.getHeader().getNumColumns (true) > 0);
  255. menu.addSeparator();
  256. }
  257. TableHeaderComponent::addMenuItems (menu, columnIdClicked);
  258. }
  259. void reactToMenuItem (int menuReturnId, int columnIdClicked)
  260. {
  261. switch (menuReturnId)
  262. {
  263. case autoSizeColumnId: owner.autoSizeColumn (columnIdClicked); break;
  264. case autoSizeAllId: owner.autoSizeAllColumns(); break;
  265. default: TableHeaderComponent::reactToMenuItem (menuReturnId, columnIdClicked); break;
  266. }
  267. }
  268. private:
  269. TableListBox& owner;
  270. enum { autoSizeColumnId = 0xf836743, autoSizeAllId = 0xf836744 };
  271. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Header)
  272. };
  273. //==============================================================================
  274. TableListBox::TableListBox (const String& name, TableListBoxModel* const m)
  275. : ListBox (name, nullptr), model (m)
  276. {
  277. ListBox::model = this;
  278. setHeader (std::make_unique<Header> (*this));
  279. }
  280. TableListBox::~TableListBox()
  281. {
  282. }
  283. void TableListBox::setModel (TableListBoxModel* newModel)
  284. {
  285. if (model != newModel)
  286. {
  287. model = newModel;
  288. updateContent();
  289. }
  290. }
  291. void TableListBox::setHeader (std::unique_ptr<TableHeaderComponent> newHeader)
  292. {
  293. if (newHeader == nullptr)
  294. {
  295. jassertfalse; // you need to supply a real header for a table!
  296. return;
  297. }
  298. Rectangle<int> newBounds (100, 28);
  299. if (header != nullptr)
  300. newBounds = header->getBounds();
  301. header = newHeader.get();
  302. header->setBounds (newBounds);
  303. setHeaderComponent (std::move (newHeader));
  304. header->addListener (this);
  305. }
  306. int TableListBox::getHeaderHeight() const noexcept
  307. {
  308. return header->getHeight();
  309. }
  310. void TableListBox::setHeaderHeight (int newHeight)
  311. {
  312. header->setSize (header->getWidth(), newHeight);
  313. resized();
  314. }
  315. void TableListBox::autoSizeColumn (int columnId)
  316. {
  317. auto width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
  318. if (width > 0)
  319. header->setColumnWidth (columnId, width);
  320. }
  321. void TableListBox::autoSizeAllColumns()
  322. {
  323. for (int i = 0; i < header->getNumColumns (true); ++i)
  324. autoSizeColumn (header->getColumnIdOfIndex (i, true));
  325. }
  326. void TableListBox::setAutoSizeMenuOptionShown (bool shouldBeShown) noexcept
  327. {
  328. autoSizeOptionsShown = shouldBeShown;
  329. }
  330. Rectangle<int> TableListBox::getCellPosition (int columnId, int rowNumber, bool relativeToComponentTopLeft) const
  331. {
  332. auto headerCell = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
  333. if (relativeToComponentTopLeft)
  334. headerCell.translate (header->getX(), 0);
  335. return getRowPosition (rowNumber, relativeToComponentTopLeft)
  336. .withX (headerCell.getX())
  337. .withWidth (headerCell.getWidth());
  338. }
  339. Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
  340. {
  341. if (auto* rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (rowNumber)))
  342. return rowComp->findChildComponentForColumn (columnId);
  343. return nullptr;
  344. }
  345. void TableListBox::scrollToEnsureColumnIsOnscreen (int columnId)
  346. {
  347. auto& scrollbar = getHorizontalScrollBar();
  348. auto pos = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
  349. auto x = scrollbar.getCurrentRangeStart();
  350. auto w = scrollbar.getCurrentRangeSize();
  351. if (pos.getX() < x)
  352. x = pos.getX();
  353. else if (pos.getRight() > x + w)
  354. x += jmax (0.0, pos.getRight() - (x + w));
  355. scrollbar.setCurrentRangeStart (x);
  356. }
  357. int TableListBox::getNumRows()
  358. {
  359. return model != nullptr ? model->getNumRows() : 0;
  360. }
  361. void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
  362. {
  363. }
  364. Component* TableListBox::refreshComponentForRow (int rowNumber, bool rowSelected, Component* existingComponentToUpdate)
  365. {
  366. if (existingComponentToUpdate == nullptr)
  367. existingComponentToUpdate = new RowComp (*this);
  368. static_cast<RowComp*> (existingComponentToUpdate)->update (rowNumber, rowSelected);
  369. return existingComponentToUpdate;
  370. }
  371. void TableListBox::selectedRowsChanged (int row)
  372. {
  373. if (model != nullptr)
  374. model->selectedRowsChanged (row);
  375. }
  376. void TableListBox::deleteKeyPressed (int row)
  377. {
  378. if (model != nullptr)
  379. model->deleteKeyPressed (row);
  380. }
  381. void TableListBox::returnKeyPressed (int row)
  382. {
  383. if (model != nullptr)
  384. model->returnKeyPressed (row);
  385. }
  386. void TableListBox::backgroundClicked (const MouseEvent& e)
  387. {
  388. if (model != nullptr)
  389. model->backgroundClicked (e);
  390. }
  391. void TableListBox::listWasScrolled()
  392. {
  393. if (model != nullptr)
  394. model->listWasScrolled();
  395. }
  396. void TableListBox::tableColumnsChanged (TableHeaderComponent*)
  397. {
  398. setMinimumContentWidth (header->getTotalWidth());
  399. repaint();
  400. updateColumnComponents();
  401. }
  402. void TableListBox::tableColumnsResized (TableHeaderComponent*)
  403. {
  404. setMinimumContentWidth (header->getTotalWidth());
  405. repaint();
  406. updateColumnComponents();
  407. }
  408. void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
  409. {
  410. if (model != nullptr)
  411. model->sortOrderChanged (header->getSortColumnId(),
  412. header->isSortedForwards());
  413. }
  414. void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
  415. {
  416. columnIdNowBeingDragged = columnIdNowBeingDragged_;
  417. repaint();
  418. }
  419. void TableListBox::resized()
  420. {
  421. ListBox::resized();
  422. header->resizeAllColumnsToFit (getVisibleContentWidth());
  423. setMinimumContentWidth (header->getTotalWidth());
  424. }
  425. void TableListBox::updateColumnComponents() const
  426. {
  427. auto firstRow = getRowContainingPosition (0, 0);
  428. for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
  429. if (auto* rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (i)))
  430. rowComp->resized();
  431. }
  432. std::unique_ptr<AccessibilityHandler> TableListBox::createAccessibilityHandler()
  433. {
  434. class TableInterface : public AccessibilityTableInterface
  435. {
  436. public:
  437. explicit TableInterface (TableListBox& tableListBoxToWrap)
  438. : tableListBox (tableListBoxToWrap)
  439. {
  440. }
  441. int getNumRows() const override
  442. {
  443. if (auto* tableModel = tableListBox.getModel())
  444. return tableModel->getNumRows();
  445. return 0;
  446. }
  447. int getNumColumns() const override
  448. {
  449. return tableListBox.getHeader().getNumColumns (false);
  450. }
  451. const AccessibilityHandler* getCellHandler (int row, int column) const override
  452. {
  453. if (isPositiveAndBelow (row, getNumRows()))
  454. {
  455. if (isPositiveAndBelow (column, getNumColumns()))
  456. if (auto* cellComponent = tableListBox.getCellComponent (tableListBox.getHeader().getColumnIdOfIndex (column, false), row))
  457. return cellComponent->getAccessibilityHandler();
  458. if (auto* rowComp = tableListBox.getComponentForRowNumber (row))
  459. return rowComp->getAccessibilityHandler();
  460. }
  461. return nullptr;
  462. }
  463. private:
  464. TableListBox& tableListBox;
  465. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableInterface)
  466. };
  467. return std::make_unique<AccessibilityHandler> (*this,
  468. AccessibilityRole::list,
  469. AccessibilityActions{},
  470. AccessibilityHandler::Interfaces { std::make_unique<TableInterface> (*this) });
  471. }
  472. //==============================================================================
  473. void TableListBoxModel::cellClicked (int, int, const MouseEvent&) {}
  474. void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&) {}
  475. void TableListBoxModel::backgroundClicked (const MouseEvent&) {}
  476. void TableListBoxModel::sortOrderChanged (int, bool) {}
  477. int TableListBoxModel::getColumnAutoSizeWidth (int) { return 0; }
  478. void TableListBoxModel::selectedRowsChanged (int) {}
  479. void TableListBoxModel::deleteKeyPressed (int) {}
  480. void TableListBoxModel::returnKeyPressed (int) {}
  481. void TableListBoxModel::listWasScrolled() {}
  482. String TableListBoxModel::getCellTooltip (int /*rowNumber*/, int /*columnId*/) { return {}; }
  483. var TableListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return {}; }
  484. Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
  485. {
  486. ignoreUnused (existingComponentToUpdate);
  487. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code that recycles the components
  488. return nullptr;
  489. }
  490. } // namespace juce