Audio plugin host https://kx.studio/carla
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.

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