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)
  107. if (TableListBoxModel* model = owner.getModel())
  108. model->cellClicked (row, columnId, e);
  109. }
  110. else
  111. {
  112. selectRowOnMouseUp = true;
  113. }
  114. }
  115. }
  116. void mouseDrag (const MouseEvent& e)
  117. {
  118. if (isEnabled() && owner.getModel() != nullptr && ! (e.mouseWasClicked() || isDragging))
  119. {
  120. const SparseSet<int> selectedRows (owner.getSelectedRows());
  121. if (selectedRows.size() > 0)
  122. {
  123. const var dragDescription (owner.getModel()->getDragSourceDescription (selectedRows));
  124. if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty())))
  125. {
  126. isDragging = true;
  127. owner.startDragAndDrop (e, dragDescription, true);
  128. }
  129. }
  130. }
  131. }
  132. void mouseUp (const MouseEvent& e)
  133. {
  134. if (selectRowOnMouseUp && e.mouseWasClicked() && isEnabled())
  135. {
  136. owner.selectRowsBasedOnModifierKeys (row, e.mods, true);
  137. const int columnId = owner.getHeader().getColumnIdAtX (e.x);
  138. if (columnId != 0)
  139. if (TableListBoxModel* model = owner.getModel())
  140. model->cellClicked (row, columnId, e);
  141. }
  142. }
  143. void mouseDoubleClick (const MouseEvent& e)
  144. {
  145. const int columnId = owner.getHeader().getColumnIdAtX (e.x);
  146. if (columnId != 0)
  147. if (TableListBoxModel* model = owner.getModel())
  148. model->cellDoubleClicked (row, columnId, e);
  149. }
  150. String getTooltip()
  151. {
  152. const int columnId = owner.getHeader().getColumnIdAtX (getMouseXYRelative().getX());
  153. if (columnId != 0)
  154. if (TableListBoxModel* model = owner.getModel())
  155. return model->getCellTooltip (row, columnId);
  156. return String::empty;
  157. }
  158. Component* findChildComponentForColumn (const int columnId) const
  159. {
  160. return columnComponents [owner.getHeader().getIndexOfColumnId (columnId, true)];
  161. }
  162. private:
  163. TableListBox& owner;
  164. OwnedArray<Component> columnComponents;
  165. int row;
  166. bool isSelected, isDragging, selectRowOnMouseUp;
  167. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RowComp)
  168. };
  169. //==============================================================================
  170. class TableListBox::Header : public TableHeaderComponent
  171. {
  172. public:
  173. Header (TableListBox& tlb) : owner (tlb) {}
  174. void addMenuItems (PopupMenu& menu, int columnIdClicked)
  175. {
  176. if (owner.isAutoSizeMenuOptionShown())
  177. {
  178. menu.addItem (autoSizeColumnId, TRANS("Auto-size this column"), columnIdClicked != 0);
  179. menu.addItem (autoSizeAllId, TRANS("Auto-size all columns"), owner.getHeader().getNumColumns (true) > 0);
  180. menu.addSeparator();
  181. }
  182. TableHeaderComponent::addMenuItems (menu, columnIdClicked);
  183. }
  184. void reactToMenuItem (int menuReturnId, int columnIdClicked)
  185. {
  186. switch (menuReturnId)
  187. {
  188. case autoSizeColumnId: owner.autoSizeColumn (columnIdClicked); break;
  189. case autoSizeAllId: owner.autoSizeAllColumns(); break;
  190. default: TableHeaderComponent::reactToMenuItem (menuReturnId, columnIdClicked); break;
  191. }
  192. }
  193. private:
  194. TableListBox& owner;
  195. enum { autoSizeColumnId = 0xf836743, autoSizeAllId = 0xf836744 };
  196. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Header)
  197. };
  198. //==============================================================================
  199. TableListBox::TableListBox (const String& name, TableListBoxModel* const m)
  200. : ListBox (name, nullptr),
  201. header (nullptr),
  202. model (m),
  203. autoSizeOptionsShown (true)
  204. {
  205. ListBox::model = this;
  206. setHeader (new Header (*this));
  207. }
  208. TableListBox::~TableListBox()
  209. {
  210. }
  211. void TableListBox::setModel (TableListBoxModel* const newModel)
  212. {
  213. if (model != newModel)
  214. {
  215. model = newModel;
  216. updateContent();
  217. }
  218. }
  219. void TableListBox::setHeader (TableHeaderComponent* newHeader)
  220. {
  221. jassert (newHeader != nullptr); // you need to supply a real header for a table!
  222. Rectangle<int> newBounds (0, 0, 100, 28);
  223. if (header != nullptr)
  224. newBounds = header->getBounds();
  225. header = newHeader;
  226. header->setBounds (newBounds);
  227. setHeaderComponent (header);
  228. header->addListener (this);
  229. }
  230. int TableListBox::getHeaderHeight() const
  231. {
  232. return header->getHeight();
  233. }
  234. void TableListBox::setHeaderHeight (const int newHeight)
  235. {
  236. header->setSize (header->getWidth(), newHeight);
  237. resized();
  238. }
  239. void TableListBox::autoSizeColumn (const int columnId)
  240. {
  241. const int width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
  242. if (width > 0)
  243. header->setColumnWidth (columnId, width);
  244. }
  245. void TableListBox::autoSizeAllColumns()
  246. {
  247. for (int i = 0; i < header->getNumColumns (true); ++i)
  248. autoSizeColumn (header->getColumnIdOfIndex (i, true));
  249. }
  250. void TableListBox::setAutoSizeMenuOptionShown (const bool shouldBeShown)
  251. {
  252. autoSizeOptionsShown = shouldBeShown;
  253. }
  254. bool TableListBox::isAutoSizeMenuOptionShown() const
  255. {
  256. return autoSizeOptionsShown;
  257. }
  258. Rectangle<int> TableListBox::getCellPosition (const int columnId, const int rowNumber,
  259. const bool relativeToComponentTopLeft) const
  260. {
  261. Rectangle<int> headerCell (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  262. if (relativeToComponentTopLeft)
  263. headerCell.translate (header->getX(), 0);
  264. return getRowPosition (rowNumber, relativeToComponentTopLeft)
  265. .withX (headerCell.getX())
  266. .withWidth (headerCell.getWidth());
  267. }
  268. Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
  269. {
  270. if (RowComp* const rowComp = dynamic_cast <RowComp*> (getComponentForRowNumber (rowNumber)))
  271. return rowComp->findChildComponentForColumn (columnId);
  272. return nullptr;
  273. }
  274. void TableListBox::scrollToEnsureColumnIsOnscreen (const int columnId)
  275. {
  276. if (ScrollBar* const scrollbar = getHorizontalScrollBar())
  277. {
  278. const Rectangle<int> pos (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  279. double x = scrollbar->getCurrentRangeStart();
  280. const double w = scrollbar->getCurrentRangeSize();
  281. if (pos.getX() < x)
  282. x = pos.getX();
  283. else if (pos.getRight() > x + w)
  284. x += jmax (0.0, pos.getRight() - (x + w));
  285. scrollbar->setCurrentRangeStart (x);
  286. }
  287. }
  288. int TableListBox::getNumRows()
  289. {
  290. return model != nullptr ? model->getNumRows() : 0;
  291. }
  292. void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
  293. {
  294. }
  295. Component* TableListBox::refreshComponentForRow (int rowNumber, bool isRowSelected_, Component* existingComponentToUpdate)
  296. {
  297. if (existingComponentToUpdate == nullptr)
  298. existingComponentToUpdate = new RowComp (*this);
  299. static_cast <RowComp*> (existingComponentToUpdate)->update (rowNumber, isRowSelected_);
  300. return existingComponentToUpdate;
  301. }
  302. void TableListBox::selectedRowsChanged (int row)
  303. {
  304. if (model != nullptr)
  305. model->selectedRowsChanged (row);
  306. }
  307. void TableListBox::deleteKeyPressed (int row)
  308. {
  309. if (model != nullptr)
  310. model->deleteKeyPressed (row);
  311. }
  312. void TableListBox::returnKeyPressed (int row)
  313. {
  314. if (model != nullptr)
  315. model->returnKeyPressed (row);
  316. }
  317. void TableListBox::backgroundClicked()
  318. {
  319. if (model != nullptr)
  320. model->backgroundClicked();
  321. }
  322. void TableListBox::listWasScrolled()
  323. {
  324. if (model != nullptr)
  325. model->listWasScrolled();
  326. }
  327. void TableListBox::tableColumnsChanged (TableHeaderComponent*)
  328. {
  329. setMinimumContentWidth (header->getTotalWidth());
  330. repaint();
  331. updateColumnComponents();
  332. }
  333. void TableListBox::tableColumnsResized (TableHeaderComponent*)
  334. {
  335. setMinimumContentWidth (header->getTotalWidth());
  336. repaint();
  337. updateColumnComponents();
  338. }
  339. void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
  340. {
  341. if (model != nullptr)
  342. model->sortOrderChanged (header->getSortColumnId(),
  343. header->isSortedForwards());
  344. }
  345. void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
  346. {
  347. columnIdNowBeingDragged = columnIdNowBeingDragged_;
  348. repaint();
  349. }
  350. void TableListBox::resized()
  351. {
  352. ListBox::resized();
  353. header->resizeAllColumnsToFit (getVisibleContentWidth());
  354. setMinimumContentWidth (header->getTotalWidth());
  355. }
  356. void TableListBox::updateColumnComponents() const
  357. {
  358. const int firstRow = getRowContainingPosition (0, 0);
  359. for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
  360. if (RowComp* const rowComp = dynamic_cast <RowComp*> (getComponentForRowNumber (i)))
  361. rowComp->resized();
  362. }
  363. //==============================================================================
  364. void TableListBoxModel::cellClicked (int, int, const MouseEvent&) {}
  365. void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&) {}
  366. void TableListBoxModel::backgroundClicked() {}
  367. void TableListBoxModel::sortOrderChanged (int, const bool) {}
  368. int TableListBoxModel::getColumnAutoSizeWidth (int) { return 0; }
  369. void TableListBoxModel::selectedRowsChanged (int) {}
  370. void TableListBoxModel::deleteKeyPressed (int) {}
  371. void TableListBoxModel::returnKeyPressed (int) {}
  372. void TableListBoxModel::listWasScrolled() {}
  373. String TableListBoxModel::getCellTooltip (int /*rowNumber*/, int /*columnId*/) { return String::empty; }
  374. var TableListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return var::null; }
  375. Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
  376. {
  377. (void) existingComponentToUpdate;
  378. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code the recycles the components
  379. return nullptr;
  380. }