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.

572 lines
18KB

  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 (*this, 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. AccessibleState getCurrentState() const override
  204. {
  205. if (auto* m = rowComponent.owner.getModel())
  206. if (rowComponent.row >= m->getNumRows())
  207. return AccessibleState().withIgnored();
  208. auto state = AccessibilityHandler::getCurrentState();
  209. if (rowComponent.owner.multipleSelection)
  210. state = state.withMultiSelectable();
  211. else
  212. state = state.withSelectable();
  213. if (rowComponent.isSelected)
  214. return state.withSelected();
  215. return state;
  216. }
  217. class RowComponentCellInterface : public AccessibilityCellInterface
  218. {
  219. public:
  220. RowComponentCellInterface (RowAccessibilityHandler& handler)
  221. : owner (handler)
  222. {
  223. }
  224. int getColumnIndex() const override { return 0; }
  225. int getColumnSpan() const override { return 1; }
  226. int getRowIndex() const override { return owner.rowComponent.row; }
  227. int getRowSpan() const override { return 1; }
  228. int getDisclosureLevel() const override { return 0; }
  229. const AccessibilityHandler* getTableHandler() const override { return owner.rowComponent.owner.getAccessibilityHandler(); }
  230. private:
  231. RowAccessibilityHandler& owner;
  232. };
  233. private:
  234. RowComp& rowComponent;
  235. };
  236. //==============================================================================
  237. TableListBox& owner;
  238. OwnedArray<Component> columnComponents;
  239. int row = -1;
  240. bool isSelected = false, isDragging = false, selectRowOnMouseUp = false;
  241. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RowComp)
  242. };
  243. //==============================================================================
  244. class TableListBox::Header : public TableHeaderComponent
  245. {
  246. public:
  247. Header (TableListBox& tlb) : owner (tlb) {}
  248. void addMenuItems (PopupMenu& menu, int columnIdClicked)
  249. {
  250. if (owner.isAutoSizeMenuOptionShown())
  251. {
  252. menu.addItem (autoSizeColumnId, TRANS("Auto-size this column"), columnIdClicked != 0);
  253. menu.addItem (autoSizeAllId, TRANS("Auto-size all columns"), owner.getHeader().getNumColumns (true) > 0);
  254. menu.addSeparator();
  255. }
  256. TableHeaderComponent::addMenuItems (menu, columnIdClicked);
  257. }
  258. void reactToMenuItem (int menuReturnId, int columnIdClicked)
  259. {
  260. switch (menuReturnId)
  261. {
  262. case autoSizeColumnId: owner.autoSizeColumn (columnIdClicked); break;
  263. case autoSizeAllId: owner.autoSizeAllColumns(); break;
  264. default: TableHeaderComponent::reactToMenuItem (menuReturnId, columnIdClicked); break;
  265. }
  266. }
  267. private:
  268. TableListBox& owner;
  269. enum { autoSizeColumnId = 0xf836743, autoSizeAllId = 0xf836744 };
  270. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Header)
  271. };
  272. //==============================================================================
  273. TableListBox::TableListBox (const String& name, TableListBoxModel* const m)
  274. : ListBox (name, nullptr), model (m)
  275. {
  276. ListBox::model = this;
  277. setHeader (std::make_unique<Header> (*this));
  278. }
  279. TableListBox::~TableListBox()
  280. {
  281. }
  282. void TableListBox::setModel (TableListBoxModel* newModel)
  283. {
  284. if (model != newModel)
  285. {
  286. model = newModel;
  287. updateContent();
  288. }
  289. }
  290. void TableListBox::setHeader (std::unique_ptr<TableHeaderComponent> newHeader)
  291. {
  292. if (newHeader == nullptr)
  293. {
  294. jassertfalse; // you need to supply a real header for a table!
  295. return;
  296. }
  297. Rectangle<int> newBounds (100, 28);
  298. if (header != nullptr)
  299. newBounds = header->getBounds();
  300. header = newHeader.get();
  301. header->setBounds (newBounds);
  302. setHeaderComponent (std::move (newHeader));
  303. header->addListener (this);
  304. }
  305. int TableListBox::getHeaderHeight() const noexcept
  306. {
  307. return header->getHeight();
  308. }
  309. void TableListBox::setHeaderHeight (int newHeight)
  310. {
  311. header->setSize (header->getWidth(), newHeight);
  312. resized();
  313. }
  314. void TableListBox::autoSizeColumn (int columnId)
  315. {
  316. auto width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
  317. if (width > 0)
  318. header->setColumnWidth (columnId, width);
  319. }
  320. void TableListBox::autoSizeAllColumns()
  321. {
  322. for (int i = 0; i < header->getNumColumns (true); ++i)
  323. autoSizeColumn (header->getColumnIdOfIndex (i, true));
  324. }
  325. void TableListBox::setAutoSizeMenuOptionShown (bool shouldBeShown) noexcept
  326. {
  327. autoSizeOptionsShown = shouldBeShown;
  328. }
  329. Rectangle<int> TableListBox::getCellPosition (int columnId, int rowNumber, bool relativeToComponentTopLeft) const
  330. {
  331. auto headerCell = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
  332. if (relativeToComponentTopLeft)
  333. headerCell.translate (header->getX(), 0);
  334. return getRowPosition (rowNumber, relativeToComponentTopLeft)
  335. .withX (headerCell.getX())
  336. .withWidth (headerCell.getWidth());
  337. }
  338. Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
  339. {
  340. if (auto* rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (rowNumber)))
  341. return rowComp->findChildComponentForColumn (columnId);
  342. return nullptr;
  343. }
  344. void TableListBox::scrollToEnsureColumnIsOnscreen (int columnId)
  345. {
  346. auto& scrollbar = getHorizontalScrollBar();
  347. auto pos = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
  348. auto x = scrollbar.getCurrentRangeStart();
  349. auto w = scrollbar.getCurrentRangeSize();
  350. if (pos.getX() < x)
  351. x = pos.getX();
  352. else if (pos.getRight() > x + w)
  353. x += jmax (0.0, pos.getRight() - (x + w));
  354. scrollbar.setCurrentRangeStart (x);
  355. }
  356. int TableListBox::getNumRows()
  357. {
  358. return model != nullptr ? model->getNumRows() : 0;
  359. }
  360. void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
  361. {
  362. }
  363. Component* TableListBox::refreshComponentForRow (int rowNumber, bool rowSelected, Component* existingComponentToUpdate)
  364. {
  365. if (existingComponentToUpdate == nullptr)
  366. existingComponentToUpdate = new RowComp (*this);
  367. static_cast<RowComp*> (existingComponentToUpdate)->update (rowNumber, rowSelected);
  368. return existingComponentToUpdate;
  369. }
  370. void TableListBox::selectedRowsChanged (int row)
  371. {
  372. if (model != nullptr)
  373. model->selectedRowsChanged (row);
  374. }
  375. void TableListBox::deleteKeyPressed (int row)
  376. {
  377. if (model != nullptr)
  378. model->deleteKeyPressed (row);
  379. }
  380. void TableListBox::returnKeyPressed (int row)
  381. {
  382. if (model != nullptr)
  383. model->returnKeyPressed (row);
  384. }
  385. void TableListBox::backgroundClicked (const MouseEvent& e)
  386. {
  387. if (model != nullptr)
  388. model->backgroundClicked (e);
  389. }
  390. void TableListBox::listWasScrolled()
  391. {
  392. if (model != nullptr)
  393. model->listWasScrolled();
  394. }
  395. void TableListBox::tableColumnsChanged (TableHeaderComponent*)
  396. {
  397. setMinimumContentWidth (header->getTotalWidth());
  398. repaint();
  399. updateColumnComponents();
  400. }
  401. void TableListBox::tableColumnsResized (TableHeaderComponent*)
  402. {
  403. setMinimumContentWidth (header->getTotalWidth());
  404. repaint();
  405. updateColumnComponents();
  406. }
  407. void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
  408. {
  409. if (model != nullptr)
  410. model->sortOrderChanged (header->getSortColumnId(),
  411. header->isSortedForwards());
  412. }
  413. void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
  414. {
  415. columnIdNowBeingDragged = columnIdNowBeingDragged_;
  416. repaint();
  417. }
  418. void TableListBox::resized()
  419. {
  420. ListBox::resized();
  421. header->resizeAllColumnsToFit (getVisibleContentWidth());
  422. setMinimumContentWidth (header->getTotalWidth());
  423. }
  424. void TableListBox::updateColumnComponents() const
  425. {
  426. auto firstRow = getRowContainingPosition (0, 0);
  427. for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
  428. if (auto* rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (i)))
  429. rowComp->resized();
  430. }
  431. std::unique_ptr<AccessibilityHandler> TableListBox::createAccessibilityHandler()
  432. {
  433. return std::make_unique<TableListBoxAccessibilityHandler> (*this);
  434. }
  435. //==============================================================================
  436. void TableListBoxModel::cellClicked (int, int, const MouseEvent&) {}
  437. void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&) {}
  438. void TableListBoxModel::backgroundClicked (const MouseEvent&) {}
  439. void TableListBoxModel::sortOrderChanged (int, bool) {}
  440. int TableListBoxModel::getColumnAutoSizeWidth (int) { return 0; }
  441. void TableListBoxModel::selectedRowsChanged (int) {}
  442. void TableListBoxModel::deleteKeyPressed (int) {}
  443. void TableListBoxModel::returnKeyPressed (int) {}
  444. void TableListBoxModel::listWasScrolled() {}
  445. String TableListBoxModel::getCellTooltip (int /*rowNumber*/, int /*columnId*/) { return {}; }
  446. var TableListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return {}; }
  447. Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
  448. {
  449. ignoreUnused (existingComponentToUpdate);
  450. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code that recycles the components
  451. return nullptr;
  452. }
  453. } // namespace juce