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.

491 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), row (-1), isSelected (false)
  24. {
  25. }
  26. void paint (Graphics& g) override
  27. {
  28. if (TableListBoxModel* const tableModel = owner.getModel())
  29. {
  30. tableModel->paintRowBackground (g, row, getWidth(), getHeight(), isSelected);
  31. const TableHeaderComponent& headerComp = owner.getHeader();
  32. const int numColumns = headerComp.getNumColumns (true);
  33. const Rectangle<int> clipBounds (g.getClipBounds());
  34. for (int i = 0; i < numColumns; ++i)
  35. {
  36. if (columnComponents[i] == nullptr)
  37. {
  38. const Rectangle<int> 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 (const int newRow, const bool isNowSelected)
  56. {
  57. jassert (newRow >= 0);
  58. if (newRow != row || isNowSelected != isSelected)
  59. {
  60. row = newRow;
  61. isSelected = isNowSelected;
  62. repaint();
  63. }
  64. TableListBoxModel* const tableModel = owner.getModel();
  65. if (tableModel != nullptr && row < owner.getNumRows())
  66. {
  67. const Identifier columnProperty ("_tableColumnId");
  68. const int numColumns = owner.getHeader().getNumColumns (true);
  69. for (int i = 0; i < numColumns; ++i)
  70. {
  71. const int columnId = owner.getHeader().getColumnIdOfIndex (i, true);
  72. Component* comp = columnComponents[i];
  73. if (comp != nullptr && columnId != (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 (const int index)
  100. {
  101. if (Component* const 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. const int columnId = owner.getHeader().getColumnIdAtX (e.x);
  115. if (columnId != 0)
  116. if (TableListBoxModel* 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. const var 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. const int 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. const int columnId = owner.getHeader().getColumnIdAtX (e.x);
  162. if (columnId != 0)
  163. if (TableListBoxModel* m = owner.getModel())
  164. m->cellDoubleClicked (row, columnId, e);
  165. }
  166. String getTooltip() override
  167. {
  168. const int 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 (const 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;
  182. bool isSelected, isDragging, selectRowOnMouseUp;
  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),
  217. header (nullptr),
  218. model (m),
  219. autoSizeOptionsShown (true)
  220. {
  221. ListBox::model = this;
  222. setHeader (new Header (*this));
  223. }
  224. TableListBox::~TableListBox()
  225. {
  226. }
  227. void TableListBox::setModel (TableListBoxModel* const newModel)
  228. {
  229. if (model != newModel)
  230. {
  231. model = newModel;
  232. updateContent();
  233. }
  234. }
  235. void TableListBox::setHeader (TableHeaderComponent* newHeader)
  236. {
  237. jassert (newHeader != nullptr); // you need to supply a real header for a table!
  238. Rectangle<int> newBounds (100, 28);
  239. if (header != nullptr)
  240. newBounds = header->getBounds();
  241. header = newHeader;
  242. header->setBounds (newBounds);
  243. setHeaderComponent (header);
  244. header->addListener (this);
  245. }
  246. int TableListBox::getHeaderHeight() const noexcept
  247. {
  248. return header->getHeight();
  249. }
  250. void TableListBox::setHeaderHeight (const int newHeight)
  251. {
  252. header->setSize (header->getWidth(), newHeight);
  253. resized();
  254. }
  255. void TableListBox::autoSizeColumn (const int columnId)
  256. {
  257. const int width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
  258. if (width > 0)
  259. header->setColumnWidth (columnId, width);
  260. }
  261. void TableListBox::autoSizeAllColumns()
  262. {
  263. for (int i = 0; i < header->getNumColumns (true); ++i)
  264. autoSizeColumn (header->getColumnIdOfIndex (i, true));
  265. }
  266. void TableListBox::setAutoSizeMenuOptionShown (const bool shouldBeShown) noexcept
  267. {
  268. autoSizeOptionsShown = shouldBeShown;
  269. }
  270. Rectangle<int> TableListBox::getCellPosition (const int columnId, const int rowNumber,
  271. const bool relativeToComponentTopLeft) const
  272. {
  273. Rectangle<int> headerCell (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  274. if (relativeToComponentTopLeft)
  275. headerCell.translate (header->getX(), 0);
  276. return getRowPosition (rowNumber, relativeToComponentTopLeft)
  277. .withX (headerCell.getX())
  278. .withWidth (headerCell.getWidth());
  279. }
  280. Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
  281. {
  282. if (RowComp* const rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (rowNumber)))
  283. return rowComp->findChildComponentForColumn (columnId);
  284. return nullptr;
  285. }
  286. void TableListBox::scrollToEnsureColumnIsOnscreen (const int columnId)
  287. {
  288. if (ScrollBar* const scrollbar = getHorizontalScrollBar())
  289. {
  290. const Rectangle<int> pos (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  291. double x = scrollbar->getCurrentRangeStart();
  292. const double w = scrollbar->getCurrentRangeSize();
  293. if (pos.getX() < x)
  294. x = pos.getX();
  295. else if (pos.getRight() > x + w)
  296. x += jmax (0.0, pos.getRight() - (x + w));
  297. scrollbar->setCurrentRangeStart (x);
  298. }
  299. }
  300. int TableListBox::getNumRows()
  301. {
  302. return model != nullptr ? model->getNumRows() : 0;
  303. }
  304. void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
  305. {
  306. }
  307. Component* TableListBox::refreshComponentForRow (int rowNumber, bool rowSelected, Component* existingComponentToUpdate)
  308. {
  309. if (existingComponentToUpdate == nullptr)
  310. existingComponentToUpdate = new RowComp (*this);
  311. static_cast<RowComp*> (existingComponentToUpdate)->update (rowNumber, rowSelected);
  312. return existingComponentToUpdate;
  313. }
  314. void TableListBox::selectedRowsChanged (int row)
  315. {
  316. if (model != nullptr)
  317. model->selectedRowsChanged (row);
  318. }
  319. void TableListBox::deleteKeyPressed (int row)
  320. {
  321. if (model != nullptr)
  322. model->deleteKeyPressed (row);
  323. }
  324. void TableListBox::returnKeyPressed (int row)
  325. {
  326. if (model != nullptr)
  327. model->returnKeyPressed (row);
  328. }
  329. void TableListBox::backgroundClicked (const MouseEvent& e)
  330. {
  331. if (model != nullptr)
  332. model->backgroundClicked (e);
  333. }
  334. void TableListBox::listWasScrolled()
  335. {
  336. if (model != nullptr)
  337. model->listWasScrolled();
  338. }
  339. void TableListBox::tableColumnsChanged (TableHeaderComponent*)
  340. {
  341. setMinimumContentWidth (header->getTotalWidth());
  342. repaint();
  343. updateColumnComponents();
  344. }
  345. void TableListBox::tableColumnsResized (TableHeaderComponent*)
  346. {
  347. setMinimumContentWidth (header->getTotalWidth());
  348. repaint();
  349. updateColumnComponents();
  350. }
  351. void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
  352. {
  353. if (model != nullptr)
  354. model->sortOrderChanged (header->getSortColumnId(),
  355. header->isSortedForwards());
  356. }
  357. void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
  358. {
  359. columnIdNowBeingDragged = columnIdNowBeingDragged_;
  360. repaint();
  361. }
  362. void TableListBox::resized()
  363. {
  364. ListBox::resized();
  365. header->resizeAllColumnsToFit (getVisibleContentWidth());
  366. setMinimumContentWidth (header->getTotalWidth());
  367. }
  368. void TableListBox::updateColumnComponents() const
  369. {
  370. const int firstRow = getRowContainingPosition (0, 0);
  371. for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
  372. if (RowComp* const rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (i)))
  373. rowComp->resized();
  374. }
  375. //==============================================================================
  376. void TableListBoxModel::cellClicked (int, int, const MouseEvent&) {}
  377. void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&) {}
  378. void TableListBoxModel::backgroundClicked (const MouseEvent&) {}
  379. void TableListBoxModel::sortOrderChanged (int, const bool) {}
  380. int TableListBoxModel::getColumnAutoSizeWidth (int) { return 0; }
  381. void TableListBoxModel::selectedRowsChanged (int) {}
  382. void TableListBoxModel::deleteKeyPressed (int) {}
  383. void TableListBoxModel::returnKeyPressed (int) {}
  384. void TableListBoxModel::listWasScrolled() {}
  385. String TableListBoxModel::getCellTooltip (int /*rowNumber*/, int /*columnId*/) { return {}; }
  386. var TableListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return {}; }
  387. Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
  388. {
  389. ignoreUnused (existingComponentToUpdate);
  390. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code that recycles the components
  391. return nullptr;
  392. }