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.

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