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.

478 lines
15KB

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