juce_TableListBox.cpp 15 KB

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