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.

928 lines
27KB

  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 TableHeaderComponent::DragOverlayComp : public Component
  19. {
  20. public:
  21. DragOverlayComp (const Image& image_)
  22. : image (image_)
  23. {
  24. image.duplicateIfShared();
  25. image.multiplyAllAlphas (0.8f);
  26. setAlwaysOnTop (true);
  27. }
  28. void paint (Graphics& g)
  29. {
  30. g.drawImageAt (image, 0, 0);
  31. }
  32. private:
  33. Image image;
  34. JUCE_DECLARE_NON_COPYABLE (DragOverlayComp)
  35. };
  36. //==============================================================================
  37. TableHeaderComponent::TableHeaderComponent()
  38. : columnsChanged (false),
  39. columnsResized (false),
  40. sortChanged (false),
  41. menuActive (true),
  42. stretchToFit (false),
  43. columnIdBeingResized (0),
  44. columnIdBeingDragged (0),
  45. columnIdUnderMouse (0),
  46. lastDeliberateWidth (0)
  47. {
  48. }
  49. TableHeaderComponent::~TableHeaderComponent()
  50. {
  51. dragOverlayComp = nullptr;
  52. }
  53. //==============================================================================
  54. void TableHeaderComponent::setPopupMenuActive (const bool hasMenu)
  55. {
  56. menuActive = hasMenu;
  57. }
  58. bool TableHeaderComponent::isPopupMenuActive() const { return menuActive; }
  59. //==============================================================================
  60. int TableHeaderComponent::getNumColumns (const bool onlyCountVisibleColumns) const
  61. {
  62. if (onlyCountVisibleColumns)
  63. {
  64. int num = 0;
  65. for (int i = columns.size(); --i >= 0;)
  66. if (columns.getUnchecked(i)->isVisible())
  67. ++num;
  68. return num;
  69. }
  70. return columns.size();
  71. }
  72. String TableHeaderComponent::getColumnName (const int columnId) const
  73. {
  74. if (const ColumnInfo* const ci = getInfoForId (columnId))
  75. return ci->name;
  76. return String::empty;
  77. }
  78. void TableHeaderComponent::setColumnName (const int columnId, const String& newName)
  79. {
  80. if (ColumnInfo* const ci = getInfoForId (columnId))
  81. {
  82. if (ci->name != newName)
  83. {
  84. ci->name = newName;
  85. sendColumnsChanged();
  86. }
  87. }
  88. }
  89. void TableHeaderComponent::addColumn (const String& columnName,
  90. const int columnId,
  91. const int width,
  92. const int minimumWidth,
  93. const int maximumWidth,
  94. const int propertyFlags,
  95. const int insertIndex)
  96. {
  97. // can't have a duplicate or null ID!
  98. jassert (columnId != 0 && getIndexOfColumnId (columnId, false) < 0);
  99. jassert (width > 0);
  100. ColumnInfo* const ci = new ColumnInfo();
  101. ci->name = columnName;
  102. ci->id = columnId;
  103. ci->width = width;
  104. ci->lastDeliberateWidth = width;
  105. ci->minimumWidth = minimumWidth;
  106. ci->maximumWidth = maximumWidth;
  107. if (ci->maximumWidth < 0)
  108. ci->maximumWidth = std::numeric_limits<int>::max();
  109. jassert (ci->maximumWidth >= ci->minimumWidth);
  110. ci->propertyFlags = propertyFlags;
  111. columns.insert (insertIndex, ci);
  112. sendColumnsChanged();
  113. }
  114. void TableHeaderComponent::removeColumn (const int columnIdToRemove)
  115. {
  116. const int index = getIndexOfColumnId (columnIdToRemove, false);
  117. if (index >= 0)
  118. {
  119. columns.remove (index);
  120. sortChanged = true;
  121. sendColumnsChanged();
  122. }
  123. }
  124. void TableHeaderComponent::removeAllColumns()
  125. {
  126. if (columns.size() > 0)
  127. {
  128. columns.clear();
  129. sendColumnsChanged();
  130. }
  131. }
  132. void TableHeaderComponent::moveColumn (const int columnId, int newIndex)
  133. {
  134. const int currentIndex = getIndexOfColumnId (columnId, false);
  135. newIndex = visibleIndexToTotalIndex (newIndex);
  136. if (columns [currentIndex] != 0 && currentIndex != newIndex)
  137. {
  138. columns.move (currentIndex, newIndex);
  139. sendColumnsChanged();
  140. }
  141. }
  142. int TableHeaderComponent::getColumnWidth (const int columnId) const
  143. {
  144. if (const ColumnInfo* const ci = getInfoForId (columnId))
  145. return ci->width;
  146. return 0;
  147. }
  148. void TableHeaderComponent::setColumnWidth (const int columnId, const int newWidth)
  149. {
  150. ColumnInfo* const ci = getInfoForId (columnId);
  151. if (ci != nullptr && ci->width != newWidth)
  152. {
  153. const int numColumns = getNumColumns (true);
  154. ci->lastDeliberateWidth = ci->width
  155. = jlimit (ci->minimumWidth, ci->maximumWidth, newWidth);
  156. if (stretchToFit)
  157. {
  158. const int index = getIndexOfColumnId (columnId, true) + 1;
  159. if (isPositiveAndBelow (index, numColumns))
  160. {
  161. const int x = getColumnPosition (index).getX();
  162. if (lastDeliberateWidth == 0)
  163. lastDeliberateWidth = getTotalWidth();
  164. resizeColumnsToFit (visibleIndexToTotalIndex (index), lastDeliberateWidth - x);
  165. }
  166. }
  167. repaint();
  168. columnsResized = true;
  169. triggerAsyncUpdate();
  170. }
  171. }
  172. //==============================================================================
  173. int TableHeaderComponent::getIndexOfColumnId (const int columnId, const bool onlyCountVisibleColumns) const
  174. {
  175. int n = 0;
  176. for (int i = 0; i < columns.size(); ++i)
  177. {
  178. if ((! onlyCountVisibleColumns) || columns.getUnchecked(i)->isVisible())
  179. {
  180. if (columns.getUnchecked(i)->id == columnId)
  181. return n;
  182. ++n;
  183. }
  184. }
  185. return -1;
  186. }
  187. int TableHeaderComponent::getColumnIdOfIndex (int index, const bool onlyCountVisibleColumns) const
  188. {
  189. if (onlyCountVisibleColumns)
  190. index = visibleIndexToTotalIndex (index);
  191. if (const ColumnInfo* const ci = columns [index])
  192. return ci->id;
  193. return 0;
  194. }
  195. Rectangle<int> TableHeaderComponent::getColumnPosition (const int index) const
  196. {
  197. int x = 0, width = 0, n = 0;
  198. for (int i = 0; i < columns.size(); ++i)
  199. {
  200. x += width;
  201. if (columns.getUnchecked(i)->isVisible())
  202. {
  203. width = columns.getUnchecked(i)->width;
  204. if (n++ == index)
  205. break;
  206. }
  207. else
  208. {
  209. width = 0;
  210. }
  211. }
  212. return Rectangle<int> (x, 0, width, getHeight());
  213. }
  214. int TableHeaderComponent::getColumnIdAtX (const int xToFind) const
  215. {
  216. if (xToFind >= 0)
  217. {
  218. int x = 0;
  219. for (int i = 0; i < columns.size(); ++i)
  220. {
  221. const ColumnInfo* const ci = columns.getUnchecked(i);
  222. if (ci->isVisible())
  223. {
  224. x += ci->width;
  225. if (xToFind < x)
  226. return ci->id;
  227. }
  228. }
  229. }
  230. return 0;
  231. }
  232. int TableHeaderComponent::getTotalWidth() const
  233. {
  234. int w = 0;
  235. for (int i = columns.size(); --i >= 0;)
  236. if (columns.getUnchecked(i)->isVisible())
  237. w += columns.getUnchecked(i)->width;
  238. return w;
  239. }
  240. void TableHeaderComponent::setStretchToFitActive (const bool shouldStretchToFit)
  241. {
  242. stretchToFit = shouldStretchToFit;
  243. lastDeliberateWidth = getTotalWidth();
  244. resized();
  245. }
  246. bool TableHeaderComponent::isStretchToFitActive() const
  247. {
  248. return stretchToFit;
  249. }
  250. void TableHeaderComponent::resizeAllColumnsToFit (int targetTotalWidth)
  251. {
  252. if (stretchToFit && getWidth() > 0
  253. && columnIdBeingResized == 0 && columnIdBeingDragged == 0)
  254. {
  255. lastDeliberateWidth = targetTotalWidth;
  256. resizeColumnsToFit (0, targetTotalWidth);
  257. }
  258. }
  259. void TableHeaderComponent::resizeColumnsToFit (int firstColumnIndex, int targetTotalWidth)
  260. {
  261. targetTotalWidth = jmax (targetTotalWidth, 0);
  262. StretchableObjectResizer sor;
  263. for (int i = firstColumnIndex; i < columns.size(); ++i)
  264. {
  265. ColumnInfo* const ci = columns.getUnchecked(i);
  266. if (ci->isVisible())
  267. sor.addItem (ci->lastDeliberateWidth, ci->minimumWidth, ci->maximumWidth);
  268. }
  269. sor.resizeToFit (targetTotalWidth);
  270. int visIndex = 0;
  271. for (int i = firstColumnIndex; i < columns.size(); ++i)
  272. {
  273. ColumnInfo* const ci = columns.getUnchecked(i);
  274. if (ci->isVisible())
  275. {
  276. const int newWidth = jlimit (ci->minimumWidth, ci->maximumWidth,
  277. (int) std::floor (sor.getItemSize (visIndex++)));
  278. if (newWidth != ci->width)
  279. {
  280. ci->width = newWidth;
  281. repaint();
  282. columnsResized = true;
  283. triggerAsyncUpdate();
  284. }
  285. }
  286. }
  287. }
  288. void TableHeaderComponent::setColumnVisible (const int columnId, const bool shouldBeVisible)
  289. {
  290. if (ColumnInfo* const ci = getInfoForId (columnId))
  291. {
  292. if (shouldBeVisible != ci->isVisible())
  293. {
  294. if (shouldBeVisible)
  295. ci->propertyFlags |= visible;
  296. else
  297. ci->propertyFlags &= ~visible;
  298. sendColumnsChanged();
  299. resized();
  300. }
  301. }
  302. }
  303. bool TableHeaderComponent::isColumnVisible (const int columnId) const
  304. {
  305. const ColumnInfo* const ci = getInfoForId (columnId);
  306. return ci != nullptr && ci->isVisible();
  307. }
  308. //==============================================================================
  309. void TableHeaderComponent::setSortColumnId (const int columnId, const bool sortForwards)
  310. {
  311. if (getSortColumnId() != columnId || isSortedForwards() != sortForwards)
  312. {
  313. for (int i = columns.size(); --i >= 0;)
  314. columns.getUnchecked(i)->propertyFlags &= ~(sortedForwards | sortedBackwards);
  315. if (ColumnInfo* const ci = getInfoForId (columnId))
  316. ci->propertyFlags |= (sortForwards ? sortedForwards : sortedBackwards);
  317. reSortTable();
  318. }
  319. }
  320. int TableHeaderComponent::getSortColumnId() const
  321. {
  322. for (int i = columns.size(); --i >= 0;)
  323. if ((columns.getUnchecked(i)->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
  324. return columns.getUnchecked(i)->id;
  325. return 0;
  326. }
  327. bool TableHeaderComponent::isSortedForwards() const
  328. {
  329. for (int i = columns.size(); --i >= 0;)
  330. if ((columns.getUnchecked(i)->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
  331. return (columns.getUnchecked(i)->propertyFlags & sortedForwards) != 0;
  332. return true;
  333. }
  334. void TableHeaderComponent::reSortTable()
  335. {
  336. sortChanged = true;
  337. repaint();
  338. triggerAsyncUpdate();
  339. }
  340. //==============================================================================
  341. String TableHeaderComponent::toString() const
  342. {
  343. String s;
  344. XmlElement doc ("TABLELAYOUT");
  345. doc.setAttribute ("sortedCol", getSortColumnId());
  346. doc.setAttribute ("sortForwards", isSortedForwards());
  347. for (int i = 0; i < columns.size(); ++i)
  348. {
  349. const ColumnInfo* const ci = columns.getUnchecked (i);
  350. XmlElement* const e = doc.createNewChildElement ("COLUMN");
  351. e->setAttribute ("id", ci->id);
  352. e->setAttribute ("visible", ci->isVisible());
  353. e->setAttribute ("width", ci->width);
  354. }
  355. return doc.createDocument (String::empty, true, false);
  356. }
  357. void TableHeaderComponent::restoreFromString (const String& storedVersion)
  358. {
  359. ScopedPointer <XmlElement> storedXml (XmlDocument::parse (storedVersion));
  360. int index = 0;
  361. if (storedXml != nullptr && storedXml->hasTagName ("TABLELAYOUT"))
  362. {
  363. forEachXmlChildElement (*storedXml, col)
  364. {
  365. const int tabId = col->getIntAttribute ("id");
  366. if (ColumnInfo* const ci = getInfoForId (tabId))
  367. {
  368. columns.move (columns.indexOf (ci), index);
  369. ci->width = col->getIntAttribute ("width");
  370. setColumnVisible (tabId, col->getBoolAttribute ("visible"));
  371. }
  372. ++index;
  373. }
  374. columnsResized = true;
  375. sendColumnsChanged();
  376. setSortColumnId (storedXml->getIntAttribute ("sortedCol"),
  377. storedXml->getBoolAttribute ("sortForwards", true));
  378. }
  379. }
  380. //==============================================================================
  381. void TableHeaderComponent::addListener (Listener* const newListener)
  382. {
  383. listeners.addIfNotAlreadyThere (newListener);
  384. }
  385. void TableHeaderComponent::removeListener (Listener* const listenerToRemove)
  386. {
  387. listeners.removeFirstMatchingValue (listenerToRemove);
  388. }
  389. //==============================================================================
  390. void TableHeaderComponent::columnClicked (int columnId, const ModifierKeys& mods)
  391. {
  392. if (const ColumnInfo* const ci = getInfoForId (columnId))
  393. if ((ci->propertyFlags & sortable) != 0 && ! mods.isPopupMenu())
  394. setSortColumnId (columnId, (ci->propertyFlags & sortedForwards) == 0);
  395. }
  396. void TableHeaderComponent::addMenuItems (PopupMenu& menu, const int /*columnIdClicked*/)
  397. {
  398. for (int i = 0; i < columns.size(); ++i)
  399. {
  400. const ColumnInfo* const ci = columns.getUnchecked(i);
  401. if ((ci->propertyFlags & appearsOnColumnMenu) != 0)
  402. menu.addItem (ci->id, ci->name,
  403. (ci->propertyFlags & (sortedForwards | sortedBackwards)) == 0,
  404. isColumnVisible (ci->id));
  405. }
  406. }
  407. void TableHeaderComponent::reactToMenuItem (const int menuReturnId, const int /*columnIdClicked*/)
  408. {
  409. if (getIndexOfColumnId (menuReturnId, false) >= 0)
  410. setColumnVisible (menuReturnId, ! isColumnVisible (menuReturnId));
  411. }
  412. void TableHeaderComponent::paint (Graphics& g)
  413. {
  414. LookAndFeel& lf = getLookAndFeel();
  415. lf.drawTableHeaderBackground (g, *this);
  416. const Rectangle<int> clip (g.getClipBounds());
  417. int x = 0;
  418. for (int i = 0; i < columns.size(); ++i)
  419. {
  420. const ColumnInfo* const ci = columns.getUnchecked(i);
  421. if (ci->isVisible())
  422. {
  423. if (x + ci->width > clip.getX()
  424. && (ci->id != columnIdBeingDragged
  425. || dragOverlayComp == nullptr
  426. || ! dragOverlayComp->isVisible()))
  427. {
  428. Graphics::ScopedSaveState ss (g);
  429. g.setOrigin (x, 0);
  430. g.reduceClipRegion (0, 0, ci->width, getHeight());
  431. lf.drawTableHeaderColumn (g, ci->name, ci->id, ci->width, getHeight(),
  432. ci->id == columnIdUnderMouse,
  433. ci->id == columnIdUnderMouse && isMouseButtonDown(),
  434. ci->propertyFlags);
  435. }
  436. x += ci->width;
  437. if (x >= clip.getRight())
  438. break;
  439. }
  440. }
  441. }
  442. void TableHeaderComponent::resized()
  443. {
  444. }
  445. void TableHeaderComponent::mouseMove (const MouseEvent& e)
  446. {
  447. updateColumnUnderMouse (e);
  448. }
  449. void TableHeaderComponent::mouseEnter (const MouseEvent& e)
  450. {
  451. updateColumnUnderMouse (e);
  452. }
  453. void TableHeaderComponent::mouseExit (const MouseEvent&)
  454. {
  455. setColumnUnderMouse (0);
  456. }
  457. void TableHeaderComponent::mouseDown (const MouseEvent& e)
  458. {
  459. repaint();
  460. columnIdBeingResized = 0;
  461. columnIdBeingDragged = 0;
  462. if (columnIdUnderMouse != 0)
  463. {
  464. draggingColumnOffset = e.x - getColumnPosition (getIndexOfColumnId (columnIdUnderMouse, true)).getX();
  465. if (e.mods.isPopupMenu())
  466. columnClicked (columnIdUnderMouse, e.mods);
  467. }
  468. if (menuActive && e.mods.isPopupMenu())
  469. showColumnChooserMenu (columnIdUnderMouse);
  470. }
  471. void TableHeaderComponent::mouseDrag (const MouseEvent& e)
  472. {
  473. if (columnIdBeingResized == 0
  474. && columnIdBeingDragged == 0
  475. && ! (e.mouseWasClicked() || e.mods.isPopupMenu()))
  476. {
  477. dragOverlayComp = nullptr;
  478. columnIdBeingResized = getResizeDraggerAt (e.getMouseDownX());
  479. if (columnIdBeingResized != 0)
  480. {
  481. const ColumnInfo* const ci = getInfoForId (columnIdBeingResized);
  482. initialColumnWidth = ci->width;
  483. }
  484. else
  485. {
  486. beginDrag (e);
  487. }
  488. }
  489. if (columnIdBeingResized != 0)
  490. {
  491. if (const ColumnInfo* const ci = getInfoForId (columnIdBeingResized))
  492. {
  493. int w = jlimit (ci->minimumWidth, ci->maximumWidth,
  494. initialColumnWidth + e.getDistanceFromDragStartX());
  495. if (stretchToFit)
  496. {
  497. // prevent us dragging a column too far right if we're in stretch-to-fit mode
  498. int minWidthOnRight = 0;
  499. for (int i = getIndexOfColumnId (columnIdBeingResized, false) + 1; i < columns.size(); ++i)
  500. if (columns.getUnchecked (i)->isVisible())
  501. minWidthOnRight += columns.getUnchecked (i)->minimumWidth;
  502. const Rectangle<int> currentPos (getColumnPosition (getIndexOfColumnId (columnIdBeingResized, true)));
  503. w = jmax (ci->minimumWidth, jmin (w, getWidth() - minWidthOnRight - currentPos.getX()));
  504. }
  505. setColumnWidth (columnIdBeingResized, w);
  506. }
  507. }
  508. else if (columnIdBeingDragged != 0)
  509. {
  510. if (e.y >= -50 && e.y < getHeight() + 50)
  511. {
  512. if (dragOverlayComp != nullptr)
  513. {
  514. dragOverlayComp->setVisible (true);
  515. dragOverlayComp->setBounds (jlimit (0,
  516. jmax (0, getTotalWidth() - dragOverlayComp->getWidth()),
  517. e.x - draggingColumnOffset),
  518. 0,
  519. dragOverlayComp->getWidth(),
  520. getHeight());
  521. for (int i = columns.size(); --i >= 0;)
  522. {
  523. const int currentIndex = getIndexOfColumnId (columnIdBeingDragged, true);
  524. int newIndex = currentIndex;
  525. if (newIndex > 0)
  526. {
  527. // if the previous column isn't draggable, we can't move our column
  528. // past it, because that'd change the undraggable column's position..
  529. const ColumnInfo* const previous = columns.getUnchecked (newIndex - 1);
  530. if ((previous->propertyFlags & draggable) != 0)
  531. {
  532. const int leftOfPrevious = getColumnPosition (newIndex - 1).getX();
  533. const int rightOfCurrent = getColumnPosition (newIndex).getRight();
  534. if (abs (dragOverlayComp->getX() - leftOfPrevious)
  535. < abs (dragOverlayComp->getRight() - rightOfCurrent))
  536. {
  537. --newIndex;
  538. }
  539. }
  540. }
  541. if (newIndex < columns.size() - 1)
  542. {
  543. // if the next column isn't draggable, we can't move our column
  544. // past it, because that'd change the undraggable column's position..
  545. const ColumnInfo* const nextCol = columns.getUnchecked (newIndex + 1);
  546. if ((nextCol->propertyFlags & draggable) != 0)
  547. {
  548. const int leftOfCurrent = getColumnPosition (newIndex).getX();
  549. const int rightOfNext = getColumnPosition (newIndex + 1).getRight();
  550. if (abs (dragOverlayComp->getX() - leftOfCurrent)
  551. > abs (dragOverlayComp->getRight() - rightOfNext))
  552. {
  553. ++newIndex;
  554. }
  555. }
  556. }
  557. if (newIndex != currentIndex)
  558. moveColumn (columnIdBeingDragged, newIndex);
  559. else
  560. break;
  561. }
  562. }
  563. }
  564. else
  565. {
  566. endDrag (draggingColumnOriginalIndex);
  567. }
  568. }
  569. }
  570. void TableHeaderComponent::beginDrag (const MouseEvent& e)
  571. {
  572. if (columnIdBeingDragged == 0)
  573. {
  574. columnIdBeingDragged = getColumnIdAtX (e.getMouseDownX());
  575. const ColumnInfo* const ci = getInfoForId (columnIdBeingDragged);
  576. if (ci == nullptr || (ci->propertyFlags & draggable) == 0)
  577. {
  578. columnIdBeingDragged = 0;
  579. }
  580. else
  581. {
  582. draggingColumnOriginalIndex = getIndexOfColumnId (columnIdBeingDragged, true);
  583. const Rectangle<int> columnRect (getColumnPosition (draggingColumnOriginalIndex));
  584. const int temp = columnIdBeingDragged;
  585. columnIdBeingDragged = 0;
  586. addAndMakeVisible (dragOverlayComp = new DragOverlayComp (createComponentSnapshot (columnRect, false)));
  587. columnIdBeingDragged = temp;
  588. dragOverlayComp->setBounds (columnRect);
  589. for (int i = listeners.size(); --i >= 0;)
  590. {
  591. listeners.getUnchecked(i)->tableColumnDraggingChanged (this, columnIdBeingDragged);
  592. i = jmin (i, listeners.size() - 1);
  593. }
  594. }
  595. }
  596. }
  597. void TableHeaderComponent::endDrag (const int finalIndex)
  598. {
  599. if (columnIdBeingDragged != 0)
  600. {
  601. moveColumn (columnIdBeingDragged, finalIndex);
  602. columnIdBeingDragged = 0;
  603. repaint();
  604. for (int i = listeners.size(); --i >= 0;)
  605. {
  606. listeners.getUnchecked(i)->tableColumnDraggingChanged (this, 0);
  607. i = jmin (i, listeners.size() - 1);
  608. }
  609. }
  610. }
  611. void TableHeaderComponent::mouseUp (const MouseEvent& e)
  612. {
  613. mouseDrag (e);
  614. for (int i = columns.size(); --i >= 0;)
  615. if (columns.getUnchecked (i)->isVisible())
  616. columns.getUnchecked (i)->lastDeliberateWidth = columns.getUnchecked (i)->width;
  617. columnIdBeingResized = 0;
  618. repaint();
  619. endDrag (getIndexOfColumnId (columnIdBeingDragged, true));
  620. updateColumnUnderMouse (e);
  621. if (columnIdUnderMouse != 0 && e.mouseWasClicked() && ! e.mods.isPopupMenu())
  622. columnClicked (columnIdUnderMouse, e.mods);
  623. dragOverlayComp = nullptr;
  624. }
  625. MouseCursor TableHeaderComponent::getMouseCursor()
  626. {
  627. if (columnIdBeingResized != 0 || (getResizeDraggerAt (getMouseXYRelative().getX()) != 0 && ! isMouseButtonDown()))
  628. return MouseCursor (MouseCursor::LeftRightResizeCursor);
  629. return Component::getMouseCursor();
  630. }
  631. //==============================================================================
  632. bool TableHeaderComponent::ColumnInfo::isVisible() const
  633. {
  634. return (propertyFlags & TableHeaderComponent::visible) != 0;
  635. }
  636. TableHeaderComponent::ColumnInfo* TableHeaderComponent::getInfoForId (const int id) const
  637. {
  638. for (int i = columns.size(); --i >= 0;)
  639. if (columns.getUnchecked(i)->id == id)
  640. return columns.getUnchecked(i);
  641. return nullptr;
  642. }
  643. int TableHeaderComponent::visibleIndexToTotalIndex (const int visibleIndex) const
  644. {
  645. int n = 0;
  646. for (int i = 0; i < columns.size(); ++i)
  647. {
  648. if (columns.getUnchecked(i)->isVisible())
  649. {
  650. if (n == visibleIndex)
  651. return i;
  652. ++n;
  653. }
  654. }
  655. return -1;
  656. }
  657. void TableHeaderComponent::sendColumnsChanged()
  658. {
  659. if (stretchToFit && lastDeliberateWidth > 0)
  660. resizeAllColumnsToFit (lastDeliberateWidth);
  661. repaint();
  662. columnsChanged = true;
  663. triggerAsyncUpdate();
  664. }
  665. void TableHeaderComponent::handleAsyncUpdate()
  666. {
  667. const bool changed = columnsChanged || sortChanged;
  668. const bool sized = columnsResized || changed;
  669. const bool sorted = sortChanged;
  670. columnsChanged = false;
  671. columnsResized = false;
  672. sortChanged = false;
  673. if (sorted)
  674. {
  675. for (int i = listeners.size(); --i >= 0;)
  676. {
  677. listeners.getUnchecked(i)->tableSortOrderChanged (this);
  678. i = jmin (i, listeners.size() - 1);
  679. }
  680. }
  681. if (changed)
  682. {
  683. for (int i = listeners.size(); --i >= 0;)
  684. {
  685. listeners.getUnchecked(i)->tableColumnsChanged (this);
  686. i = jmin (i, listeners.size() - 1);
  687. }
  688. }
  689. if (sized)
  690. {
  691. for (int i = listeners.size(); --i >= 0;)
  692. {
  693. listeners.getUnchecked(i)->tableColumnsResized (this);
  694. i = jmin (i, listeners.size() - 1);
  695. }
  696. }
  697. }
  698. int TableHeaderComponent::getResizeDraggerAt (const int mouseX) const
  699. {
  700. if (isPositiveAndBelow (mouseX, getWidth()))
  701. {
  702. const int draggableDistance = 3;
  703. int x = 0;
  704. for (int i = 0; i < columns.size(); ++i)
  705. {
  706. const ColumnInfo* const ci = columns.getUnchecked(i);
  707. if (ci->isVisible())
  708. {
  709. if (abs (mouseX - (x + ci->width)) <= draggableDistance
  710. && (ci->propertyFlags & resizable) != 0)
  711. return ci->id;
  712. x += ci->width;
  713. }
  714. }
  715. }
  716. return 0;
  717. }
  718. void TableHeaderComponent::setColumnUnderMouse (const int newCol)
  719. {
  720. if (newCol != columnIdUnderMouse)
  721. {
  722. columnIdUnderMouse = newCol;
  723. repaint();
  724. }
  725. }
  726. void TableHeaderComponent::updateColumnUnderMouse (const MouseEvent& e)
  727. {
  728. setColumnUnderMouse (reallyContains (e.getPosition(), true) && getResizeDraggerAt (e.x) == 0
  729. ? getColumnIdAtX (e.x) : 0);
  730. }
  731. static void tableHeaderMenuCallback (int result, TableHeaderComponent* tableHeader, int columnIdClicked)
  732. {
  733. if (tableHeader != nullptr && result != 0)
  734. tableHeader->reactToMenuItem (result, columnIdClicked);
  735. }
  736. void TableHeaderComponent::showColumnChooserMenu (const int columnIdClicked)
  737. {
  738. PopupMenu m;
  739. addMenuItems (m, columnIdClicked);
  740. if (m.getNumItems() > 0)
  741. {
  742. m.setLookAndFeel (&getLookAndFeel());
  743. m.showMenuAsync (PopupMenu::Options(),
  744. ModalCallbackFunction::forComponent (tableHeaderMenuCallback, this, columnIdClicked));
  745. }
  746. }
  747. void TableHeaderComponent::Listener::tableColumnDraggingChanged (TableHeaderComponent*, int)
  748. {
  749. }