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.

489 lines
15KB

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