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.

896 lines
26KB

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