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.

588 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. #include "../../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_TableListBox.h"
  21. #include "../../../containers/juce_BitArray.h"
  22. #include "../../../core/juce_Random.h"
  23. #include "../mouse/juce_DragAndDropContainer.h"
  24. #include "../../graphics/imaging/juce_Image.h"
  25. #include "../../../text/juce_LocalisedStrings.h"
  26. //==============================================================================
  27. static const tchar* const tableColumnPropertyTag = T("_tableColumnID");
  28. class TableListRowComp : public Component,
  29. public TooltipClient
  30. {
  31. public:
  32. TableListRowComp (TableListBox& owner_)
  33. : owner (owner_),
  34. row (-1),
  35. isSelected (false)
  36. {
  37. }
  38. ~TableListRowComp()
  39. {
  40. deleteAllChildren();
  41. }
  42. void paint (Graphics& g)
  43. {
  44. TableListBoxModel* const model = owner.getModel();
  45. if (model != 0)
  46. {
  47. const TableHeaderComponent* const header = owner.getHeader();
  48. model->paintRowBackground (g, row, getWidth(), getHeight(), isSelected);
  49. const int numColumns = header->getNumColumns (true);
  50. for (int i = 0; i < numColumns; ++i)
  51. {
  52. if (! columnsWithComponents [i])
  53. {
  54. const int columnId = header->getColumnIdOfIndex (i, true);
  55. Rectangle columnRect (header->getColumnPosition (i));
  56. columnRect.setSize (columnRect.getWidth(), getHeight());
  57. g.saveState();
  58. g.reduceClipRegion (columnRect);
  59. g.setOrigin (columnRect.getX(), 0);
  60. model->paintCell (g, row, columnId, columnRect.getWidth(), columnRect.getHeight(), isSelected);
  61. g.restoreState();
  62. }
  63. }
  64. }
  65. }
  66. void update (const int newRow, const bool isNowSelected)
  67. {
  68. if (newRow != row || isNowSelected != isSelected)
  69. {
  70. row = newRow;
  71. isSelected = isNowSelected;
  72. repaint();
  73. }
  74. if (row < owner.getNumRows())
  75. {
  76. jassert (row >= 0);
  77. const tchar* const tagPropertyName = T("_tableLastUseNum");
  78. const int newTag = Random::getSystemRandom().nextInt();
  79. const TableHeaderComponent* const header = owner.getHeader();
  80. const int numColumns = header->getNumColumns (true);
  81. int i;
  82. columnsWithComponents.clear();
  83. if (owner.getModel() != 0)
  84. {
  85. for (i = 0; i < numColumns; ++i)
  86. {
  87. const int columnId = header->getColumnIdOfIndex (i, true);
  88. Component* const newComp
  89. = owner.getModel()->refreshComponentForCell (row, columnId, isSelected,
  90. findChildComponentForColumn (columnId));
  91. if (newComp != 0)
  92. {
  93. addAndMakeVisible (newComp);
  94. newComp->setComponentProperty (tagPropertyName, newTag);
  95. newComp->setComponentProperty (tableColumnPropertyTag, columnId);
  96. const Rectangle columnRect (header->getColumnPosition (i));
  97. newComp->setBounds (columnRect.getX(), 0, columnRect.getWidth(), getHeight());
  98. columnsWithComponents.setBit (i);
  99. }
  100. }
  101. }
  102. for (i = getNumChildComponents(); --i >= 0;)
  103. {
  104. Component* const c = getChildComponent (i);
  105. if (c->getComponentPropertyInt (tagPropertyName, false, 0) != newTag)
  106. delete c;
  107. }
  108. }
  109. else
  110. {
  111. columnsWithComponents.clear();
  112. deleteAllChildren();
  113. }
  114. }
  115. void resized()
  116. {
  117. for (int i = getNumChildComponents(); --i >= 0;)
  118. {
  119. Component* const c = getChildComponent (i);
  120. const int columnId = c->getComponentPropertyInt (tableColumnPropertyTag, false, 0);
  121. if (columnId != 0)
  122. {
  123. const Rectangle columnRect (owner.getHeader()->getColumnPosition (owner.getHeader()->getIndexOfColumnId (columnId, true)));
  124. c->setBounds (columnRect.getX(), 0, columnRect.getWidth(), getHeight());
  125. }
  126. }
  127. }
  128. void mouseDown (const MouseEvent& e)
  129. {
  130. isDragging = false;
  131. selectRowOnMouseUp = false;
  132. if (isEnabled())
  133. {
  134. if (! isSelected)
  135. {
  136. owner.selectRowsBasedOnModifierKeys (row, e.mods);
  137. const int columnId = owner.getHeader()->getColumnIdAtX (e.x);
  138. if (columnId != 0 && owner.getModel() != 0)
  139. owner.getModel()->cellClicked (row, columnId, e);
  140. }
  141. else
  142. {
  143. selectRowOnMouseUp = true;
  144. }
  145. }
  146. }
  147. void mouseDrag (const MouseEvent& e)
  148. {
  149. if (isEnabled() && owner.getModel() != 0 && ! (e.mouseWasClicked() || isDragging))
  150. {
  151. const SparseSet <int> selectedRows (owner.getSelectedRows());
  152. if (selectedRows.size() > 0)
  153. {
  154. const String dragDescription (owner.getModel()->getDragSourceDescription (selectedRows));
  155. if (dragDescription.isNotEmpty())
  156. {
  157. isDragging = true;
  158. DragAndDropContainer* const dragContainer
  159. = DragAndDropContainer::findParentDragContainerFor (this);
  160. if (dragContainer != 0)
  161. {
  162. Image* dragImage = owner.createSnapshotOfSelectedRows();
  163. dragImage->multiplyAllAlphas (0.6f);
  164. dragContainer->startDragging (dragDescription, &owner, dragImage, true);
  165. }
  166. else
  167. {
  168. // to be able to do a drag-and-drop operation, the listbox needs to
  169. // be inside a component which is also a DragAndDropContainer.
  170. jassertfalse
  171. }
  172. }
  173. }
  174. }
  175. }
  176. void mouseUp (const MouseEvent& e)
  177. {
  178. if (selectRowOnMouseUp && e.mouseWasClicked() && isEnabled())
  179. {
  180. owner.selectRowsBasedOnModifierKeys (row, e.mods);
  181. const int columnId = owner.getHeader()->getColumnIdAtX (e.x);
  182. if (columnId != 0 && owner.getModel() != 0)
  183. owner.getModel()->cellClicked (row, columnId, e);
  184. }
  185. }
  186. void mouseDoubleClick (const MouseEvent& e)
  187. {
  188. const int columnId = owner.getHeader()->getColumnIdAtX (e.x);
  189. if (columnId != 0 && owner.getModel() != 0)
  190. owner.getModel()->cellDoubleClicked (row, columnId, e);
  191. }
  192. const String getTooltip()
  193. {
  194. int x, y;
  195. getMouseXYRelative (x, y);
  196. const int columnId = owner.getHeader()->getColumnIdAtX (x);
  197. if (columnId != 0 && owner.getModel() != 0)
  198. return owner.getModel()->getCellTooltip (row, columnId);
  199. return String::empty;
  200. }
  201. juce_UseDebuggingNewOperator
  202. private:
  203. TableListBox& owner;
  204. int row;
  205. bool isSelected, isDragging, selectRowOnMouseUp;
  206. BitArray columnsWithComponents;
  207. Component* findChildComponentForColumn (const int columnId) const
  208. {
  209. for (int i = getNumChildComponents(); --i >= 0;)
  210. {
  211. Component* const c = getChildComponent (i);
  212. if (c->getComponentPropertyInt (tableColumnPropertyTag, false, 0) == columnId)
  213. return c;
  214. }
  215. return 0;
  216. }
  217. TableListRowComp (const TableListRowComp&);
  218. const TableListRowComp& operator= (const TableListRowComp&);
  219. };
  220. //==============================================================================
  221. class TableListBoxHeader : public TableHeaderComponent
  222. {
  223. public:
  224. TableListBoxHeader (TableListBox& owner_)
  225. : owner (owner_)
  226. {
  227. }
  228. ~TableListBoxHeader()
  229. {
  230. }
  231. void addMenuItems (PopupMenu& menu, const int columnIdClicked)
  232. {
  233. if (owner.isAutoSizeMenuOptionShown())
  234. {
  235. menu.addItem (0xf836743, TRANS("Auto-size this column"), columnIdClicked != 0);
  236. menu.addItem (0xf836744, TRANS("Auto-size all columns"), owner.getHeader()->getNumColumns (true) > 0);
  237. menu.addSeparator();
  238. }
  239. TableHeaderComponent::addMenuItems (menu, columnIdClicked);
  240. }
  241. void reactToMenuItem (const int menuReturnId, const int columnIdClicked)
  242. {
  243. if (menuReturnId == 0xf836743)
  244. {
  245. owner.autoSizeColumn (columnIdClicked);
  246. }
  247. else if (menuReturnId == 0xf836744)
  248. {
  249. owner.autoSizeAllColumns();
  250. }
  251. else
  252. {
  253. TableHeaderComponent::reactToMenuItem (menuReturnId, columnIdClicked);
  254. }
  255. }
  256. juce_UseDebuggingNewOperator
  257. private:
  258. TableListBox& owner;
  259. TableListBoxHeader (const TableListBoxHeader&);
  260. const TableListBoxHeader& operator= (const TableListBoxHeader&);
  261. };
  262. //==============================================================================
  263. TableListBox::TableListBox (const String& name, TableListBoxModel* const model_)
  264. : ListBox (name, 0),
  265. model (model_),
  266. autoSizeOptionsShown (true)
  267. {
  268. ListBox::model = this;
  269. header = new TableListBoxHeader (*this);
  270. header->setSize (100, 28);
  271. header->addListener (this);
  272. setHeaderComponent (header);
  273. }
  274. TableListBox::~TableListBox()
  275. {
  276. deleteAllChildren();
  277. }
  278. void TableListBox::setModel (TableListBoxModel* const newModel)
  279. {
  280. if (model != newModel)
  281. {
  282. model = newModel;
  283. updateContent();
  284. }
  285. }
  286. int TableListBox::getHeaderHeight() const throw()
  287. {
  288. return header->getHeight();
  289. }
  290. void TableListBox::setHeaderHeight (const int newHeight)
  291. {
  292. header->setSize (header->getWidth(), newHeight);
  293. resized();
  294. }
  295. void TableListBox::autoSizeColumn (const int columnId)
  296. {
  297. const int width = model != 0 ? model->getColumnAutoSizeWidth (columnId) : 0;
  298. if (width > 0)
  299. header->setColumnWidth (columnId, width);
  300. }
  301. void TableListBox::autoSizeAllColumns()
  302. {
  303. for (int i = 0; i < header->getNumColumns (true); ++i)
  304. autoSizeColumn (header->getColumnIdOfIndex (i, true));
  305. }
  306. void TableListBox::setAutoSizeMenuOptionShown (const bool shouldBeShown)
  307. {
  308. autoSizeOptionsShown = shouldBeShown;
  309. }
  310. bool TableListBox::isAutoSizeMenuOptionShown() const throw()
  311. {
  312. return autoSizeOptionsShown;
  313. }
  314. const Rectangle TableListBox::getCellPosition (const int columnId,
  315. const int rowNumber,
  316. const bool relativeToComponentTopLeft) const
  317. {
  318. Rectangle headerCell (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  319. if (relativeToComponentTopLeft)
  320. headerCell.translate (header->getX(), 0);
  321. const Rectangle row (getRowPosition (rowNumber, relativeToComponentTopLeft));
  322. return Rectangle (headerCell.getX(), row.getY(),
  323. headerCell.getWidth(), row.getHeight());
  324. }
  325. void TableListBox::scrollToEnsureColumnIsOnscreen (const int columnId)
  326. {
  327. ScrollBar* const scrollbar = getHorizontalScrollBar();
  328. if (scrollbar != 0)
  329. {
  330. const Rectangle pos (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));
  331. double x = scrollbar->getCurrentRangeStart();
  332. const double w = scrollbar->getCurrentRangeSize();
  333. if (pos.getX() < x)
  334. x = pos.getX();
  335. else if (pos.getRight() > x + w)
  336. x += jmax (0.0, pos.getRight() - (x + w));
  337. scrollbar->setCurrentRangeStart (x);
  338. }
  339. }
  340. int TableListBox::getNumRows()
  341. {
  342. return model != 0 ? model->getNumRows() : 0;
  343. }
  344. void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
  345. {
  346. }
  347. Component* TableListBox::refreshComponentForRow (int rowNumber, bool isRowSelected_, Component* existingComponentToUpdate)
  348. {
  349. if (existingComponentToUpdate == 0)
  350. existingComponentToUpdate = new TableListRowComp (*this);
  351. ((TableListRowComp*) existingComponentToUpdate)->update (rowNumber, isRowSelected_);
  352. return existingComponentToUpdate;
  353. }
  354. void TableListBox::selectedRowsChanged (int row)
  355. {
  356. if (model != 0)
  357. model->selectedRowsChanged (row);
  358. }
  359. void TableListBox::deleteKeyPressed (int row)
  360. {
  361. if (model != 0)
  362. model->deleteKeyPressed (row);
  363. }
  364. void TableListBox::returnKeyPressed (int row)
  365. {
  366. if (model != 0)
  367. model->returnKeyPressed (row);
  368. }
  369. void TableListBox::backgroundClicked()
  370. {
  371. if (model != 0)
  372. model->backgroundClicked();
  373. }
  374. void TableListBox::listWasScrolled()
  375. {
  376. if (model != 0)
  377. model->listWasScrolled();
  378. }
  379. void TableListBox::tableColumnsChanged (TableHeaderComponent*)
  380. {
  381. setMinimumContentWidth (header->getTotalWidth());
  382. repaint();
  383. updateColumnComponents();
  384. }
  385. void TableListBox::tableColumnsResized (TableHeaderComponent*)
  386. {
  387. setMinimumContentWidth (header->getTotalWidth());
  388. repaint();
  389. updateColumnComponents();
  390. }
  391. void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
  392. {
  393. if (model != 0)
  394. model->sortOrderChanged (header->getSortColumnId(),
  395. header->isSortedForwards());
  396. }
  397. void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
  398. {
  399. columnIdNowBeingDragged = columnIdNowBeingDragged_;
  400. repaint();
  401. }
  402. void TableListBox::resized()
  403. {
  404. ListBox::resized();
  405. header->resizeAllColumnsToFit (getVisibleContentWidth());
  406. setMinimumContentWidth (header->getTotalWidth());
  407. }
  408. void TableListBox::updateColumnComponents() const
  409. {
  410. const int firstRow = getRowContainingPosition (0, 0);
  411. for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
  412. {
  413. TableListRowComp* const rowComp = dynamic_cast <TableListRowComp*> (getComponentForRowNumber (i));
  414. if (rowComp != 0)
  415. rowComp->resized();
  416. }
  417. }
  418. //==============================================================================
  419. void TableListBoxModel::cellClicked (int, int, const MouseEvent&)
  420. {
  421. }
  422. void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&)
  423. {
  424. }
  425. void TableListBoxModel::backgroundClicked()
  426. {
  427. }
  428. void TableListBoxModel::sortOrderChanged (int, const bool)
  429. {
  430. }
  431. int TableListBoxModel::getColumnAutoSizeWidth (int)
  432. {
  433. return 0;
  434. }
  435. void TableListBoxModel::selectedRowsChanged (int)
  436. {
  437. }
  438. void TableListBoxModel::deleteKeyPressed (int)
  439. {
  440. }
  441. void TableListBoxModel::returnKeyPressed (int)
  442. {
  443. }
  444. void TableListBoxModel::listWasScrolled()
  445. {
  446. }
  447. const String TableListBoxModel::getCellTooltip (int /*rowNumber*/, int /*columnId*/)
  448. {
  449. return String::empty;
  450. }
  451. const String TableListBoxModel::getDragSourceDescription (const SparseSet<int>&)
  452. {
  453. return String::empty;
  454. }
  455. Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
  456. {
  457. (void) existingComponentToUpdate;
  458. jassert (existingComponentToUpdate == 0); // indicates a failure in the code the recycles the components
  459. return 0;
  460. }
  461. END_JUCE_NAMESPACE