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) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class TableListBox::RowComp : public Component,
  18. public TooltipClient
  19. {
  20. public:
  21. RowComp (TableListBox& tlb) noexcept : owner (tlb), row (-1), isSelected (false)
  22. {
  23. }
  24. void paint (Graphics& g) override
  25. {
  26. if (TableListBoxModel* const tableModel = owner.getModel())
  27. {
  28. tableModel->paintRowBackground (g, row, getWidth(), getHeight(), isSelected);
  29. const TableHeaderComponent& headerComp = owner.getHeader();
  30. const int numColumns = headerComp.getNumColumns (true);
  31. const Rectangle<int> clipBounds (g.getClipBounds());
  32. for (int i = 0; i < numColumns; ++i)
  33. {
  34. if (columnComponents[i] == nullptr)
  35. {
  36. const Rectangle<int> 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 (const int newRow, const bool isNowSelected)
  54. {
  55. jassert (newRow >= 0);
  56. if (newRow != row || isNowSelected != isSelected)
  57. {
  58. row = newRow;
  59. isSelected = isNowSelected;
  60. repaint();
  61. }
  62. TableListBoxModel* const tableModel = owner.getModel();
  63. if (tableModel != nullptr && row < owner.getNumRows())
  64. {
  65. const Identifier columnProperty ("_tableColumnId");
  66. const int numColumns = owner.getHeader().getNumColumns (true);
  67. for (int i = 0; i < numColumns; ++i)
  68. {
  69. const int columnId = owner.getHeader().getColumnIdOfIndex (i, true);
  70. Component* comp = columnComponents[i];
  71. if (comp != nullptr && columnId != (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 (const int index)
  98. {
  99. if (Component* const 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. const int columnId = owner.getHeader().getColumnIdAtX (e.x);
  113. if (columnId != 0)
  114. if (TableListBoxModel* 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. const var 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. const int 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. const int columnId = owner.getHeader().getColumnIdAtX (e.x);
  160. if (columnId != 0)
  161. if (TableListBoxModel* m = owner.getModel())
  162. m->cellDoubleClicked (row, columnId, e);
  163. }
  164. String getTooltip() override
  165. {
  166. const int columnId = owner.getHeader().getColumnIdAtX (getMouseXYRelative().getX());
  167. if (columnId != 0)
  168. if (TableListBoxModel* m = owner.getModel())
  169. return m->getCellTooltip (row, columnId);
  170. return String();
  171. }
  172. Component* findChildComponentForColumn (const 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;
  180. bool isSelected, isDragging, selectRowOnMouseUp;
  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),
  215. header (nullptr),
  216. model (m),
  217. autoSizeOptionsShown (true)
  218. {
  219. ListBox::model = this;
  220. setHeader (new Header (*this));
  221. }
  222. TableListBox::~TableListBox()
  223. {
  224. }
  225. void TableListBox::setModel (TableListBoxModel* const newModel)
  226. {
  227. if (model != newModel)
  228. {
  229. model = newModel;
  230. updateContent();
  231. }
  232. }
  233. void TableListBox::setHeader (TableHeaderComponent* newHeader)
  234. {
  235. jassert (newHeader != nullptr); // you need to supply a real header for a table!
  236. Rectangle<int> newBounds (100, 28);
  237. if (header != nullptr)
  238. newBounds = header->getBounds();
  239. header = newHeader;
  240. header->setBounds (newBounds);
  241. setHeaderComponent (header);
  242. header->addListener (this);
  243. }
  244. int TableListBox::getHeaderHeight() const noexcept
  245. {
  246. return header->getHeight();
  247. }
  248. void TableListBox::setHeaderHeight (const int newHeight)
  249. {
  250. header->setSize (header->getWidth(), newHeight);
  251. resized();
  252. }
  253. void TableListBox::autoSizeColumn (const int columnId)
  254. {
  255. const int width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
  256. if (width > 0)
  257. header->setColumnWidth (columnId, width);
  258. }
  259. void TableListBox::autoSizeAllColumns()
  260. {
  261. for (int i = 0; i < header->getNumColumns (true); ++i)
  262. autoSizeColumn (header->getColumnIdOfIndex (i, true));
  263. }
  264. void TableListBox::setAutoSizeMenuOptionShown (const bool shouldBeShown) noexcept
  265. {
  266. autoSizeOptionsShown = shouldBeShown;
  267. }
  268. Rectangle<int> TableListBox::getCellPosition (const int columnId, const int rowNumber,
  269. const bool relativeToComponentTopLeft) const
  270. {
  271. Rectangle<int> headerCell (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  272. if (relativeToComponentTopLeft)
  273. headerCell.translate (header->getX(), 0);
  274. return getRowPosition (rowNumber, relativeToComponentTopLeft)
  275. .withX (headerCell.getX())
  276. .withWidth (headerCell.getWidth());
  277. }
  278. Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
  279. {
  280. if (RowComp* const rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (rowNumber)))
  281. return rowComp->findChildComponentForColumn (columnId);
  282. return nullptr;
  283. }
  284. void TableListBox::scrollToEnsureColumnIsOnscreen (const int columnId)
  285. {
  286. if (ScrollBar* const scrollbar = getHorizontalScrollBar())
  287. {
  288. const Rectangle<int> pos (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  289. double x = scrollbar->getCurrentRangeStart();
  290. const double w = scrollbar->getCurrentRangeSize();
  291. if (pos.getX() < x)
  292. x = pos.getX();
  293. else if (pos.getRight() > x + w)
  294. x += jmax (0.0, pos.getRight() - (x + w));
  295. scrollbar->setCurrentRangeStart (x);
  296. }
  297. }
  298. int TableListBox::getNumRows()
  299. {
  300. return model != nullptr ? model->getNumRows() : 0;
  301. }
  302. void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
  303. {
  304. }
  305. Component* TableListBox::refreshComponentForRow (int rowNumber, bool rowSelected, Component* existingComponentToUpdate)
  306. {
  307. if (existingComponentToUpdate == nullptr)
  308. existingComponentToUpdate = new RowComp (*this);
  309. static_cast<RowComp*> (existingComponentToUpdate)->update (rowNumber, rowSelected);
  310. return existingComponentToUpdate;
  311. }
  312. void TableListBox::selectedRowsChanged (int row)
  313. {
  314. if (model != nullptr)
  315. model->selectedRowsChanged (row);
  316. }
  317. void TableListBox::deleteKeyPressed (int row)
  318. {
  319. if (model != nullptr)
  320. model->deleteKeyPressed (row);
  321. }
  322. void TableListBox::returnKeyPressed (int row)
  323. {
  324. if (model != nullptr)
  325. model->returnKeyPressed (row);
  326. }
  327. void TableListBox::backgroundClicked (const MouseEvent& e)
  328. {
  329. if (model != nullptr)
  330. model->backgroundClicked (e);
  331. }
  332. void TableListBox::listWasScrolled()
  333. {
  334. if (model != nullptr)
  335. model->listWasScrolled();
  336. }
  337. void TableListBox::tableColumnsChanged (TableHeaderComponent*)
  338. {
  339. setMinimumContentWidth (header->getTotalWidth());
  340. repaint();
  341. updateColumnComponents();
  342. }
  343. void TableListBox::tableColumnsResized (TableHeaderComponent*)
  344. {
  345. setMinimumContentWidth (header->getTotalWidth());
  346. repaint();
  347. updateColumnComponents();
  348. }
  349. void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
  350. {
  351. if (model != nullptr)
  352. model->sortOrderChanged (header->getSortColumnId(),
  353. header->isSortedForwards());
  354. }
  355. void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
  356. {
  357. columnIdNowBeingDragged = columnIdNowBeingDragged_;
  358. repaint();
  359. }
  360. void TableListBox::resized()
  361. {
  362. ListBox::resized();
  363. header->resizeAllColumnsToFit (getVisibleContentWidth());
  364. setMinimumContentWidth (header->getTotalWidth());
  365. }
  366. void TableListBox::updateColumnComponents() const
  367. {
  368. const int firstRow = getRowContainingPosition (0, 0);
  369. for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
  370. if (RowComp* const rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (i)))
  371. rowComp->resized();
  372. }
  373. //==============================================================================
  374. void TableListBoxModel::cellClicked (int, int, const MouseEvent&) {}
  375. void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&) {}
  376. void TableListBoxModel::backgroundClicked (const MouseEvent&) {}
  377. void TableListBoxModel::sortOrderChanged (int, const bool) {}
  378. int TableListBoxModel::getColumnAutoSizeWidth (int) { return 0; }
  379. void TableListBoxModel::selectedRowsChanged (int) {}
  380. void TableListBoxModel::deleteKeyPressed (int) {}
  381. void TableListBoxModel::returnKeyPressed (int) {}
  382. void TableListBoxModel::listWasScrolled() {}
  383. String TableListBoxModel::getCellTooltip (int /*rowNumber*/, int /*columnId*/) { return String(); }
  384. var TableListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return var(); }
  385. Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
  386. {
  387. ignoreUnused (existingComponentToUpdate);
  388. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code that recycles the components
  389. return nullptr;
  390. }