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.

486 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. class TableListBox::RowComp : public Component,
  20. public TooltipClient
  21. {
  22. public:
  23. RowComp (TableListBox& tlb) noexcept : owner (tlb) {}
  24. void paint (Graphics& g) override
  25. {
  26. if (auto* tableModel = owner.getModel())
  27. {
  28. tableModel->paintRowBackground (g, row, getWidth(), getHeight(), isSelected);
  29. auto& headerComp = owner.getHeader();
  30. auto numColumns = headerComp.getNumColumns (true);
  31. auto clipBounds = g.getClipBounds();
  32. for (int i = 0; i < numColumns; ++i)
  33. {
  34. if (columnComponents[i] == nullptr)
  35. {
  36. auto columnRect = headerComp.getColumnPosition(i).withHeight (getHeight());
  37. if (columnRect.getX() >= clipBounds.getRight())
  38. break;
  39. if (columnRect.getRight() > clipBounds.getX())
  40. {
  41. Graphics::ScopedSaveState ss (g);
  42. if (g.reduceClipRegion (columnRect))
  43. {
  44. g.setOrigin (columnRect.getX(), 0);
  45. tableModel->paintCell (g, row, headerComp.getColumnIdOfIndex (i, true),
  46. columnRect.getWidth(), columnRect.getHeight(), isSelected);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. void update (int newRow, bool isNowSelected)
  54. {
  55. jassert (newRow >= 0);
  56. if (newRow != row || isNowSelected != isSelected)
  57. {
  58. row = newRow;
  59. isSelected = isNowSelected;
  60. repaint();
  61. }
  62. auto* tableModel = owner.getModel();
  63. if (tableModel != nullptr && row < owner.getNumRows())
  64. {
  65. const Identifier columnProperty ("_tableColumnId");
  66. auto numColumns = owner.getHeader().getNumColumns (true);
  67. for (int i = 0; i < numColumns; ++i)
  68. {
  69. auto columnId = owner.getHeader().getColumnIdOfIndex (i, true);
  70. auto* comp = columnComponents[i];
  71. if (comp != nullptr && columnId != static_cast<int> (comp->getProperties() [columnProperty]))
  72. {
  73. columnComponents.set (i, nullptr);
  74. comp = nullptr;
  75. }
  76. comp = tableModel->refreshComponentForCell (row, columnId, isSelected, comp);
  77. columnComponents.set (i, comp, false);
  78. if (comp != nullptr)
  79. {
  80. comp->getProperties().set (columnProperty, columnId);
  81. addAndMakeVisible (comp);
  82. resizeCustomComp (i);
  83. }
  84. }
  85. columnComponents.removeRange (numColumns, columnComponents.size());
  86. }
  87. else
  88. {
  89. columnComponents.clear();
  90. }
  91. }
  92. void resized() override
  93. {
  94. for (int i = columnComponents.size(); --i >= 0;)
  95. resizeCustomComp (i);
  96. }
  97. void resizeCustomComp (int index)
  98. {
  99. if (auto* c = columnComponents.getUnchecked (index))
  100. c->setBounds (owner.getHeader().getColumnPosition (index)
  101. .withY (0).withHeight (getHeight()));
  102. }
  103. void mouseDown (const MouseEvent& e) override
  104. {
  105. isDragging = false;
  106. selectRowOnMouseUp = false;
  107. if (isEnabled())
  108. {
  109. if (! isSelected)
  110. {
  111. owner.selectRowsBasedOnModifierKeys (row, e.mods, false);
  112. auto columnId = owner.getHeader().getColumnIdAtX (e.x);
  113. if (columnId != 0)
  114. if (auto* m = owner.getModel())
  115. m->cellClicked (row, columnId, e);
  116. }
  117. else
  118. {
  119. selectRowOnMouseUp = true;
  120. }
  121. }
  122. }
  123. void mouseDrag (const MouseEvent& e) override
  124. {
  125. if (isEnabled()
  126. && owner.getModel() != nullptr
  127. && e.mouseWasDraggedSinceMouseDown()
  128. && ! isDragging)
  129. {
  130. SparseSet<int> rowsToDrag;
  131. if (owner.selectOnMouseDown || owner.isRowSelected (row))
  132. rowsToDrag = owner.getSelectedRows();
  133. else
  134. rowsToDrag.addRange (Range<int>::withStartAndLength (row, 1));
  135. if (rowsToDrag.size() > 0)
  136. {
  137. auto dragDescription = owner.getModel()->getDragSourceDescription (rowsToDrag);
  138. if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty())))
  139. {
  140. isDragging = true;
  141. owner.startDragAndDrop (e, rowsToDrag, dragDescription, true);
  142. }
  143. }
  144. }
  145. }
  146. void mouseUp (const MouseEvent& e) override
  147. {
  148. if (selectRowOnMouseUp && e.mouseWasClicked() && isEnabled())
  149. {
  150. owner.selectRowsBasedOnModifierKeys (row, e.mods, true);
  151. auto columnId = owner.getHeader().getColumnIdAtX (e.x);
  152. if (columnId != 0)
  153. if (TableListBoxModel* m = owner.getModel())
  154. m->cellClicked (row, columnId, e);
  155. }
  156. }
  157. void mouseDoubleClick (const MouseEvent& e) override
  158. {
  159. auto columnId = owner.getHeader().getColumnIdAtX (e.x);
  160. if (columnId != 0)
  161. if (auto* m = owner.getModel())
  162. m->cellDoubleClicked (row, columnId, e);
  163. }
  164. String getTooltip() override
  165. {
  166. auto columnId = owner.getHeader().getColumnIdAtX (getMouseXYRelative().getX());
  167. if (columnId != 0)
  168. if (auto* m = owner.getModel())
  169. return m->getCellTooltip (row, columnId);
  170. return {};
  171. }
  172. Component* findChildComponentForColumn (int columnId) const
  173. {
  174. return columnComponents [owner.getHeader().getIndexOfColumnId (columnId, true)];
  175. }
  176. private:
  177. TableListBox& owner;
  178. OwnedArray<Component> columnComponents;
  179. int row = -1;
  180. bool isSelected = false, isDragging = false, selectRowOnMouseUp = false;
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RowComp)
  182. };
  183. //==============================================================================
  184. class TableListBox::Header : public TableHeaderComponent
  185. {
  186. public:
  187. Header (TableListBox& tlb) : owner (tlb) {}
  188. void addMenuItems (PopupMenu& menu, int columnIdClicked)
  189. {
  190. if (owner.isAutoSizeMenuOptionShown())
  191. {
  192. menu.addItem (autoSizeColumnId, TRANS("Auto-size this column"), columnIdClicked != 0);
  193. menu.addItem (autoSizeAllId, TRANS("Auto-size all columns"), owner.getHeader().getNumColumns (true) > 0);
  194. menu.addSeparator();
  195. }
  196. TableHeaderComponent::addMenuItems (menu, columnIdClicked);
  197. }
  198. void reactToMenuItem (int menuReturnId, int columnIdClicked)
  199. {
  200. switch (menuReturnId)
  201. {
  202. case autoSizeColumnId: owner.autoSizeColumn (columnIdClicked); break;
  203. case autoSizeAllId: owner.autoSizeAllColumns(); break;
  204. default: TableHeaderComponent::reactToMenuItem (menuReturnId, columnIdClicked); break;
  205. }
  206. }
  207. private:
  208. TableListBox& owner;
  209. enum { autoSizeColumnId = 0xf836743, autoSizeAllId = 0xf836744 };
  210. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Header)
  211. };
  212. //==============================================================================
  213. TableListBox::TableListBox (const String& name, TableListBoxModel* const m)
  214. : ListBox (name, nullptr), model (m)
  215. {
  216. ListBox::model = this;
  217. setHeader (new Header (*this));
  218. }
  219. TableListBox::~TableListBox()
  220. {
  221. }
  222. void TableListBox::setModel (TableListBoxModel* newModel)
  223. {
  224. if (model != newModel)
  225. {
  226. model = newModel;
  227. updateContent();
  228. }
  229. }
  230. void TableListBox::setHeader (TableHeaderComponent* newHeader)
  231. {
  232. jassert (newHeader != nullptr); // you need to supply a real header for a table!
  233. Rectangle<int> newBounds (100, 28);
  234. if (header != nullptr)
  235. newBounds = header->getBounds();
  236. header = newHeader;
  237. header->setBounds (newBounds);
  238. setHeaderComponent (header);
  239. header->addListener (this);
  240. }
  241. int TableListBox::getHeaderHeight() const noexcept
  242. {
  243. return header->getHeight();
  244. }
  245. void TableListBox::setHeaderHeight (int newHeight)
  246. {
  247. header->setSize (header->getWidth(), newHeight);
  248. resized();
  249. }
  250. void TableListBox::autoSizeColumn (int columnId)
  251. {
  252. auto width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
  253. if (width > 0)
  254. header->setColumnWidth (columnId, width);
  255. }
  256. void TableListBox::autoSizeAllColumns()
  257. {
  258. for (int i = 0; i < header->getNumColumns (true); ++i)
  259. autoSizeColumn (header->getColumnIdOfIndex (i, true));
  260. }
  261. void TableListBox::setAutoSizeMenuOptionShown (bool shouldBeShown) noexcept
  262. {
  263. autoSizeOptionsShown = shouldBeShown;
  264. }
  265. Rectangle<int> TableListBox::getCellPosition (int columnId, int rowNumber, bool relativeToComponentTopLeft) const
  266. {
  267. auto headerCell = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
  268. if (relativeToComponentTopLeft)
  269. headerCell.translate (header->getX(), 0);
  270. return getRowPosition (rowNumber, relativeToComponentTopLeft)
  271. .withX (headerCell.getX())
  272. .withWidth (headerCell.getWidth());
  273. }
  274. Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
  275. {
  276. if (auto* rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (rowNumber)))
  277. return rowComp->findChildComponentForColumn (columnId);
  278. return nullptr;
  279. }
  280. void TableListBox::scrollToEnsureColumnIsOnscreen (int columnId)
  281. {
  282. if (auto* scrollbar = getHorizontalScrollBar())
  283. {
  284. auto pos = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
  285. auto x = scrollbar->getCurrentRangeStart();
  286. auto w = scrollbar->getCurrentRangeSize();
  287. if (pos.getX() < x)
  288. x = pos.getX();
  289. else if (pos.getRight() > x + w)
  290. x += jmax (0.0, pos.getRight() - (x + w));
  291. scrollbar->setCurrentRangeStart (x);
  292. }
  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. }