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.

491 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. }
  215. void TableListBox::setModel (TableListBoxModel* const newModel)
  216. {
  217. if (model != newModel)
  218. {
  219. model = newModel;
  220. updateContent();
  221. }
  222. }
  223. void TableListBox::setHeader (TableHeaderComponent* newHeader)
  224. {
  225. jassert (newHeader != nullptr); // you need to supply a real header for a table!
  226. Rectangle<int> newBounds (0, 0, 100, 28);
  227. if (header != nullptr)
  228. newBounds = header->getBounds();
  229. header = newHeader;
  230. header->setBounds (newBounds);
  231. setHeaderComponent (header);
  232. header->addListener (this);
  233. }
  234. int TableListBox::getHeaderHeight() const
  235. {
  236. return header->getHeight();
  237. }
  238. void TableListBox::setHeaderHeight (const int newHeight)
  239. {
  240. header->setSize (header->getWidth(), newHeight);
  241. resized();
  242. }
  243. void TableListBox::autoSizeColumn (const int columnId)
  244. {
  245. const int width = model != nullptr ? model->getColumnAutoSizeWidth (columnId) : 0;
  246. if (width > 0)
  247. header->setColumnWidth (columnId, width);
  248. }
  249. void TableListBox::autoSizeAllColumns()
  250. {
  251. for (int i = 0; i < header->getNumColumns (true); ++i)
  252. autoSizeColumn (header->getColumnIdOfIndex (i, true));
  253. }
  254. void TableListBox::setAutoSizeMenuOptionShown (const bool shouldBeShown)
  255. {
  256. autoSizeOptionsShown = shouldBeShown;
  257. }
  258. bool TableListBox::isAutoSizeMenuOptionShown() const
  259. {
  260. return autoSizeOptionsShown;
  261. }
  262. Rectangle<int> TableListBox::getCellPosition (const int columnId, const int rowNumber,
  263. const bool relativeToComponentTopLeft) const
  264. {
  265. Rectangle<int> headerCell (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  266. if (relativeToComponentTopLeft)
  267. headerCell.translate (header->getX(), 0);
  268. return getRowPosition (rowNumber, relativeToComponentTopLeft)
  269. .withX (headerCell.getX())
  270. .withWidth (headerCell.getWidth());
  271. }
  272. Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
  273. {
  274. TableListRowComp* const rowComp = dynamic_cast <TableListRowComp*> (getComponentForRowNumber (rowNumber));
  275. return rowComp != nullptr ? rowComp->findChildComponentForColumn (columnId) : 0;
  276. }
  277. void TableListBox::scrollToEnsureColumnIsOnscreen (const int columnId)
  278. {
  279. ScrollBar* const scrollbar = getHorizontalScrollBar();
  280. if (scrollbar != nullptr)
  281. {
  282. const Rectangle<int> pos (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  283. double x = scrollbar->getCurrentRangeStart();
  284. const double w = scrollbar->getCurrentRangeSize();
  285. if (pos.getX() < x)
  286. x = pos.getX();
  287. else if (pos.getRight() > x + w)
  288. x += jmax (0.0, pos.getRight() - (x + w));
  289. scrollbar->setCurrentRangeStart (x);
  290. }
  291. }
  292. int TableListBox::getNumRows()
  293. {
  294. return model != nullptr ? model->getNumRows() : 0;
  295. }
  296. void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
  297. {
  298. }
  299. Component* TableListBox::refreshComponentForRow (int rowNumber, bool isRowSelected_, Component* existingComponentToUpdate)
  300. {
  301. if (existingComponentToUpdate == nullptr)
  302. existingComponentToUpdate = new TableListRowComp (*this);
  303. static_cast <TableListRowComp*> (existingComponentToUpdate)->update (rowNumber, isRowSelected_);
  304. return existingComponentToUpdate;
  305. }
  306. void TableListBox::selectedRowsChanged (int row)
  307. {
  308. if (model != nullptr)
  309. model->selectedRowsChanged (row);
  310. }
  311. void TableListBox::deleteKeyPressed (int row)
  312. {
  313. if (model != nullptr)
  314. model->deleteKeyPressed (row);
  315. }
  316. void TableListBox::returnKeyPressed (int row)
  317. {
  318. if (model != nullptr)
  319. model->returnKeyPressed (row);
  320. }
  321. void TableListBox::backgroundClicked()
  322. {
  323. if (model != nullptr)
  324. model->backgroundClicked();
  325. }
  326. void TableListBox::listWasScrolled()
  327. {
  328. if (model != nullptr)
  329. model->listWasScrolled();
  330. }
  331. void TableListBox::tableColumnsChanged (TableHeaderComponent*)
  332. {
  333. setMinimumContentWidth (header->getTotalWidth());
  334. repaint();
  335. updateColumnComponents();
  336. }
  337. void TableListBox::tableColumnsResized (TableHeaderComponent*)
  338. {
  339. setMinimumContentWidth (header->getTotalWidth());
  340. repaint();
  341. updateColumnComponents();
  342. }
  343. void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
  344. {
  345. if (model != nullptr)
  346. model->sortOrderChanged (header->getSortColumnId(),
  347. header->isSortedForwards());
  348. }
  349. void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
  350. {
  351. columnIdNowBeingDragged = columnIdNowBeingDragged_;
  352. repaint();
  353. }
  354. void TableListBox::resized()
  355. {
  356. ListBox::resized();
  357. header->resizeAllColumnsToFit (getVisibleContentWidth());
  358. setMinimumContentWidth (header->getTotalWidth());
  359. }
  360. void TableListBox::updateColumnComponents() const
  361. {
  362. const int firstRow = getRowContainingPosition (0, 0);
  363. for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
  364. {
  365. TableListRowComp* const rowComp = dynamic_cast <TableListRowComp*> (getComponentForRowNumber (i));
  366. if (rowComp != nullptr)
  367. rowComp->resized();
  368. }
  369. }
  370. //==============================================================================
  371. void TableListBoxModel::cellClicked (int, int, const MouseEvent&) {}
  372. void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&) {}
  373. void TableListBoxModel::backgroundClicked() {}
  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::empty; }
  381. var TableListBoxModel::getDragSourceDescription (const SparseSet<int>&) { return var::null; }
  382. Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
  383. {
  384. (void) existingComponentToUpdate;
  385. jassert (existingComponentToUpdate == nullptr); // indicates a failure in the code the recycles the components
  386. return nullptr;
  387. }
  388. END_JUCE_NAMESPACE