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) 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() && owner.getModel() != nullptr && ! (e.mouseWasClicked() || isDragging))
  126. {
  127. SparseSet<int> rowsToDrag;
  128. if (owner.selectOnMouseDown || owner.isRowSelected (row))
  129. rowsToDrag = owner.getSelectedRows();
  130. else
  131. rowsToDrag.addRange (Range<int>::withStartAndLength (row, 1));
  132. if (rowsToDrag.size() > 0)
  133. {
  134. const var dragDescription (owner.getModel()->getDragSourceDescription (rowsToDrag));
  135. if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty())))
  136. {
  137. isDragging = true;
  138. owner.startDragAndDrop (e, rowsToDrag, dragDescription, true);
  139. }
  140. }
  141. }
  142. }
  143. void mouseUp (const MouseEvent& e) override
  144. {
  145. if (selectRowOnMouseUp && e.mouseWasClicked() && isEnabled())
  146. {
  147. owner.selectRowsBasedOnModifierKeys (row, e.mods, true);
  148. const int columnId = owner.getHeader().getColumnIdAtX (e.x);
  149. if (columnId != 0)
  150. if (TableListBoxModel* m = owner.getModel())
  151. m->cellClicked (row, columnId, e);
  152. }
  153. }
  154. void mouseDoubleClick (const MouseEvent& e) override
  155. {
  156. const int columnId = owner.getHeader().getColumnIdAtX (e.x);
  157. if (columnId != 0)
  158. if (TableListBoxModel* m = owner.getModel())
  159. m->cellDoubleClicked (row, columnId, e);
  160. }
  161. String getTooltip() override
  162. {
  163. const int columnId = owner.getHeader().getColumnIdAtX (getMouseXYRelative().getX());
  164. if (columnId != 0)
  165. if (TableListBoxModel* m = owner.getModel())
  166. return m->getCellTooltip (row, columnId);
  167. return String();
  168. }
  169. Component* findChildComponentForColumn (const int columnId) const
  170. {
  171. return columnComponents [owner.getHeader().getIndexOfColumnId (columnId, true)];
  172. }
  173. private:
  174. TableListBox& owner;
  175. OwnedArray<Component> columnComponents;
  176. int row;
  177. bool isSelected, isDragging, selectRowOnMouseUp;
  178. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RowComp)
  179. };
  180. //==============================================================================
  181. class TableListBox::Header : public TableHeaderComponent
  182. {
  183. public:
  184. Header (TableListBox& tlb) : owner (tlb) {}
  185. void addMenuItems (PopupMenu& menu, int columnIdClicked)
  186. {
  187. if (owner.isAutoSizeMenuOptionShown())
  188. {
  189. menu.addItem (autoSizeColumnId, TRANS("Auto-size this column"), columnIdClicked != 0);
  190. menu.addItem (autoSizeAllId, TRANS("Auto-size all columns"), owner.getHeader().getNumColumns (true) > 0);
  191. menu.addSeparator();
  192. }
  193. TableHeaderComponent::addMenuItems (menu, columnIdClicked);
  194. }
  195. void reactToMenuItem (int menuReturnId, int columnIdClicked)
  196. {
  197. switch (menuReturnId)
  198. {
  199. case autoSizeColumnId: owner.autoSizeColumn (columnIdClicked); break;
  200. case autoSizeAllId: owner.autoSizeAllColumns(); break;
  201. default: TableHeaderComponent::reactToMenuItem (menuReturnId, columnIdClicked); break;
  202. }
  203. }
  204. private:
  205. TableListBox& owner;
  206. enum { autoSizeColumnId = 0xf836743, autoSizeAllId = 0xf836744 };
  207. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Header)
  208. };
  209. //==============================================================================
  210. TableListBox::TableListBox (const String& name, TableListBoxModel* const m)
  211. : ListBox (name, nullptr),
  212. header (nullptr),
  213. model (m),
  214. autoSizeOptionsShown (true)
  215. {
  216. ListBox::model = this;
  217. setHeader (new Header (*this));
  218. }
  219. TableListBox::~TableListBox()
  220. {
  221. }
  222. void TableListBox::setModel (TableListBoxModel* const 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 (const int newHeight)
  246. {
  247. header->setSize (header->getWidth(), newHeight);
  248. resized();
  249. }
  250. void TableListBox::autoSizeColumn (const int columnId)
  251. {
  252. const int 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 (const bool shouldBeShown) noexcept
  262. {
  263. autoSizeOptionsShown = shouldBeShown;
  264. }
  265. Rectangle<int> TableListBox::getCellPosition (const int columnId, const int rowNumber,
  266. const bool relativeToComponentTopLeft) const
  267. {
  268. Rectangle<int> headerCell (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  269. if (relativeToComponentTopLeft)
  270. headerCell.translate (header->getX(), 0);
  271. return getRowPosition (rowNumber, relativeToComponentTopLeft)
  272. .withX (headerCell.getX())
  273. .withWidth (headerCell.getWidth());
  274. }
  275. Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
  276. {
  277. if (RowComp* const rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (rowNumber)))
  278. return rowComp->findChildComponentForColumn (columnId);
  279. return nullptr;
  280. }
  281. void TableListBox::scrollToEnsureColumnIsOnscreen (const int columnId)
  282. {
  283. if (ScrollBar* const scrollbar = getHorizontalScrollBar())
  284. {
  285. const Rectangle<int> pos (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  286. double x = scrollbar->getCurrentRangeStart();
  287. const double 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. }
  295. int TableListBox::getNumRows()
  296. {
  297. return model != nullptr ? model->getNumRows() : 0;
  298. }
  299. void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
  300. {
  301. }
  302. Component* TableListBox::refreshComponentForRow (int rowNumber, bool rowSelected, Component* existingComponentToUpdate)
  303. {
  304. if (existingComponentToUpdate == nullptr)
  305. existingComponentToUpdate = new RowComp (*this);
  306. static_cast<RowComp*> (existingComponentToUpdate)->update (rowNumber, rowSelected);
  307. return existingComponentToUpdate;
  308. }
  309. void TableListBox::selectedRowsChanged (int row)
  310. {
  311. if (model != nullptr)
  312. model->selectedRowsChanged (row);
  313. }
  314. void TableListBox::deleteKeyPressed (int row)
  315. {
  316. if (model != nullptr)
  317. model->deleteKeyPressed (row);
  318. }
  319. void TableListBox::returnKeyPressed (int row)
  320. {
  321. if (model != nullptr)
  322. model->returnKeyPressed (row);
  323. }
  324. void TableListBox::backgroundClicked (const MouseEvent& e)
  325. {
  326. if (model != nullptr)
  327. model->backgroundClicked (e);
  328. }
  329. void TableListBox::listWasScrolled()
  330. {
  331. if (model != nullptr)
  332. model->listWasScrolled();
  333. }
  334. void TableListBox::tableColumnsChanged (TableHeaderComponent*)
  335. {
  336. setMinimumContentWidth (header->getTotalWidth());
  337. repaint();
  338. updateColumnComponents();
  339. }
  340. void TableListBox::tableColumnsResized (TableHeaderComponent*)
  341. {
  342. setMinimumContentWidth (header->getTotalWidth());
  343. repaint();
  344. updateColumnComponents();
  345. }
  346. void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
  347. {
  348. if (model != nullptr)
  349. model->sortOrderChanged (header->getSortColumnId(),
  350. header->isSortedForwards());
  351. }
  352. void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
  353. {
  354. columnIdNowBeingDragged = columnIdNowBeingDragged_;
  355. repaint();
  356. }
  357. void TableListBox::resized()
  358. {
  359. ListBox::resized();
  360. header->resizeAllColumnsToFit (getVisibleContentWidth());
  361. setMinimumContentWidth (header->getTotalWidth());
  362. }
  363. void TableListBox::updateColumnComponents() const
  364. {
  365. const int firstRow = getRowContainingPosition (0, 0);
  366. for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
  367. if (RowComp* const rowComp = dynamic_cast<RowComp*> (getComponentForRowNumber (i)))
  368. rowComp->resized();
  369. }
  370. //==============================================================================
  371. void TableListBoxModel::cellClicked (int, int, const MouseEvent&) {}
  372. void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&) {}
  373. void TableListBoxModel::backgroundClicked (const MouseEvent&) {}
  374. void TableListBoxModel::sortOrderChanged (int, const bool) {}
  375. int TableListBoxModel::getColumnAutoSizeWidth (int) { return 0; }
  376. void TableListBoxModel::selectedRowsChanged (int) {}
  377. void TableListBoxModel::deleteKeyPressed (int) {}
  378. void TableListBoxModel::returnKeyPressed (int) {}
  379. void TableListBoxModel::listWasScrolled() {}
  380. String TableListBoxModel::getCellTooltip (int /*rowNumber*/, int /*columnId*/) { return String(); }
  381. var TableListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return var(); }
  382. Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
  383. {
  384. ignoreUnused (existingComponentToUpdate);
  385. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code that recycles the components
  386. return nullptr;
  387. }