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.

907 lines
26KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. class TableHeaderComponent::DragOverlayComp : public Component
  21. {
  22. public:
  23. DragOverlayComp (const Image& i) : image (i)
  24. {
  25. image.duplicateIfShared();
  26. image.multiplyAllAlphas (0.8f);
  27. setAlwaysOnTop (true);
  28. }
  29. void paint (Graphics& g) override
  30. {
  31. g.drawImage (image, getLocalBounds().toFloat());
  32. }
  33. Image image;
  34. JUCE_DECLARE_NON_COPYABLE (DragOverlayComp)
  35. };
  36. //==============================================================================
  37. TableHeaderComponent::TableHeaderComponent()
  38. {
  39. }
  40. TableHeaderComponent::~TableHeaderComponent()
  41. {
  42. dragOverlayComp.reset();
  43. }
  44. //==============================================================================
  45. void TableHeaderComponent::setPopupMenuActive (bool hasMenu)
  46. {
  47. menuActive = hasMenu;
  48. }
  49. bool TableHeaderComponent::isPopupMenuActive() const { return menuActive; }
  50. //==============================================================================
  51. int TableHeaderComponent::getNumColumns (const bool onlyCountVisibleColumns) const
  52. {
  53. if (onlyCountVisibleColumns)
  54. {
  55. int num = 0;
  56. for (auto* c : columns)
  57. if (c->isVisible())
  58. ++num;
  59. return num;
  60. }
  61. return columns.size();
  62. }
  63. String TableHeaderComponent::getColumnName (const int columnId) const
  64. {
  65. if (auto* ci = getInfoForId (columnId))
  66. return ci->name;
  67. return {};
  68. }
  69. void TableHeaderComponent::setColumnName (const int columnId, const String& newName)
  70. {
  71. if (auto* ci = getInfoForId (columnId))
  72. {
  73. if (ci->name != newName)
  74. {
  75. ci->name = newName;
  76. sendColumnsChanged();
  77. }
  78. }
  79. }
  80. void TableHeaderComponent::addColumn (const String& columnName,
  81. int columnId,
  82. int width,
  83. int minimumWidth,
  84. int maximumWidth,
  85. int propertyFlags,
  86. int insertIndex)
  87. {
  88. // can't have a duplicate or zero ID!
  89. jassert (columnId != 0 && getIndexOfColumnId (columnId, false) < 0);
  90. jassert (width > 0);
  91. auto ci = new ColumnInfo();
  92. ci->name = columnName;
  93. ci->id = columnId;
  94. ci->width = width;
  95. ci->lastDeliberateWidth = width;
  96. ci->minimumWidth = minimumWidth;
  97. ci->maximumWidth = maximumWidth >= 0 ? maximumWidth : std::numeric_limits<int>::max();
  98. jassert (ci->maximumWidth >= ci->minimumWidth);
  99. ci->propertyFlags = propertyFlags;
  100. columns.insert (insertIndex, ci);
  101. sendColumnsChanged();
  102. }
  103. void TableHeaderComponent::removeColumn (const int columnIdToRemove)
  104. {
  105. auto index = getIndexOfColumnId (columnIdToRemove, false);
  106. if (index >= 0)
  107. {
  108. columns.remove (index);
  109. sortChanged = true;
  110. sendColumnsChanged();
  111. }
  112. }
  113. void TableHeaderComponent::removeAllColumns()
  114. {
  115. if (columns.size() > 0)
  116. {
  117. columns.clear();
  118. sendColumnsChanged();
  119. }
  120. }
  121. void TableHeaderComponent::moveColumn (const int columnId, int newIndex)
  122. {
  123. auto currentIndex = getIndexOfColumnId (columnId, false);
  124. newIndex = visibleIndexToTotalIndex (newIndex);
  125. if (columns[currentIndex] != nullptr && currentIndex != newIndex)
  126. {
  127. columns.move (currentIndex, newIndex);
  128. sendColumnsChanged();
  129. }
  130. }
  131. int TableHeaderComponent::getColumnWidth (const int columnId) const
  132. {
  133. if (auto* ci = getInfoForId (columnId))
  134. return ci->width;
  135. return 0;
  136. }
  137. void TableHeaderComponent::setColumnWidth (const int columnId, const int newWidth)
  138. {
  139. if (auto* ci = getInfoForId (columnId))
  140. {
  141. const auto newWidthToUse = jlimit (ci->minimumWidth, ci->maximumWidth, newWidth);
  142. if (ci->width != newWidthToUse)
  143. {
  144. auto numColumns = getNumColumns (true);
  145. ci->lastDeliberateWidth = ci->width = newWidthToUse;
  146. if (stretchToFit)
  147. {
  148. auto index = getIndexOfColumnId (columnId, true) + 1;
  149. if (isPositiveAndBelow (index, numColumns))
  150. {
  151. auto x = getColumnPosition (index).getX();
  152. if (lastDeliberateWidth == 0)
  153. lastDeliberateWidth = getTotalWidth();
  154. resizeColumnsToFit (visibleIndexToTotalIndex (index), lastDeliberateWidth - x);
  155. }
  156. }
  157. repaint();
  158. columnsResized = true;
  159. triggerAsyncUpdate();
  160. }
  161. }
  162. }
  163. //==============================================================================
  164. int TableHeaderComponent::getIndexOfColumnId (const int columnId, const bool onlyCountVisibleColumns) const
  165. {
  166. int n = 0;
  167. for (auto* c : columns)
  168. {
  169. if ((! onlyCountVisibleColumns) || c->isVisible())
  170. {
  171. if (c->id == columnId)
  172. return n;
  173. ++n;
  174. }
  175. }
  176. return -1;
  177. }
  178. int TableHeaderComponent::getColumnIdOfIndex (int index, const bool onlyCountVisibleColumns) const
  179. {
  180. if (onlyCountVisibleColumns)
  181. index = visibleIndexToTotalIndex (index);
  182. if (auto* ci = columns [index])
  183. return ci->id;
  184. return 0;
  185. }
  186. Rectangle<int> TableHeaderComponent::getColumnPosition (const int index) const
  187. {
  188. int x = 0, width = 0, n = 0;
  189. for (auto* c : columns)
  190. {
  191. x += width;
  192. if (c->isVisible())
  193. {
  194. width = c->width;
  195. if (n++ == index)
  196. break;
  197. }
  198. else
  199. {
  200. width = 0;
  201. }
  202. }
  203. return { x, 0, width, getHeight() };
  204. }
  205. int TableHeaderComponent::getColumnIdAtX (const int xToFind) const
  206. {
  207. if (xToFind >= 0)
  208. {
  209. int x = 0;
  210. for (auto* ci : columns)
  211. {
  212. if (ci->isVisible())
  213. {
  214. x += ci->width;
  215. if (xToFind < x)
  216. return ci->id;
  217. }
  218. }
  219. }
  220. return 0;
  221. }
  222. int TableHeaderComponent::getTotalWidth() const
  223. {
  224. int w = 0;
  225. for (auto* c : columns)
  226. if (c->isVisible())
  227. w += c->width;
  228. return w;
  229. }
  230. void TableHeaderComponent::setStretchToFitActive (const bool shouldStretchToFit)
  231. {
  232. stretchToFit = shouldStretchToFit;
  233. lastDeliberateWidth = getTotalWidth();
  234. resized();
  235. }
  236. bool TableHeaderComponent::isStretchToFitActive() const
  237. {
  238. return stretchToFit;
  239. }
  240. void TableHeaderComponent::resizeAllColumnsToFit (int targetTotalWidth)
  241. {
  242. if (stretchToFit && getWidth() > 0
  243. && columnIdBeingResized == 0 && columnIdBeingDragged == 0)
  244. {
  245. lastDeliberateWidth = targetTotalWidth;
  246. resizeColumnsToFit (0, targetTotalWidth);
  247. }
  248. }
  249. void TableHeaderComponent::resizeColumnsToFit (int firstColumnIndex, int targetTotalWidth)
  250. {
  251. targetTotalWidth = jmax (targetTotalWidth, 0);
  252. StretchableObjectResizer sor;
  253. for (int i = firstColumnIndex; i < columns.size(); ++i)
  254. {
  255. auto* ci = columns.getUnchecked(i);
  256. if (ci->isVisible())
  257. sor.addItem (ci->lastDeliberateWidth, ci->minimumWidth, ci->maximumWidth);
  258. }
  259. sor.resizeToFit (targetTotalWidth);
  260. int visIndex = 0;
  261. for (int i = firstColumnIndex; i < columns.size(); ++i)
  262. {
  263. auto* ci = columns.getUnchecked(i);
  264. if (ci->isVisible())
  265. {
  266. auto newWidth = jlimit (ci->minimumWidth, ci->maximumWidth,
  267. (int) std::floor (sor.getItemSize (visIndex++)));
  268. if (newWidth != ci->width)
  269. {
  270. ci->width = newWidth;
  271. repaint();
  272. columnsResized = true;
  273. triggerAsyncUpdate();
  274. }
  275. }
  276. }
  277. }
  278. void TableHeaderComponent::setColumnVisible (const int columnId, const bool shouldBeVisible)
  279. {
  280. if (auto* ci = getInfoForId (columnId))
  281. {
  282. if (shouldBeVisible != ci->isVisible())
  283. {
  284. if (shouldBeVisible)
  285. ci->propertyFlags |= visible;
  286. else
  287. ci->propertyFlags &= ~visible;
  288. sendColumnsChanged();
  289. resized();
  290. }
  291. }
  292. }
  293. bool TableHeaderComponent::isColumnVisible (const int columnId) const
  294. {
  295. if (auto* ci = getInfoForId (columnId))
  296. return ci->isVisible();
  297. return false;
  298. }
  299. //==============================================================================
  300. void TableHeaderComponent::setSortColumnId (const int columnId, const bool sortForwards)
  301. {
  302. if (getSortColumnId() != columnId || isSortedForwards() != sortForwards)
  303. {
  304. for (auto* c : columns)
  305. c->propertyFlags &= ~(sortedForwards | sortedBackwards);
  306. if (auto* ci = getInfoForId (columnId))
  307. ci->propertyFlags |= (sortForwards ? sortedForwards : sortedBackwards);
  308. reSortTable();
  309. }
  310. }
  311. int TableHeaderComponent::getSortColumnId() const
  312. {
  313. for (auto* c : columns)
  314. if ((c->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
  315. return c->id;
  316. return 0;
  317. }
  318. bool TableHeaderComponent::isSortedForwards() const
  319. {
  320. for (auto* c : columns)
  321. if ((c->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
  322. return (c->propertyFlags & sortedForwards) != 0;
  323. return true;
  324. }
  325. void TableHeaderComponent::reSortTable()
  326. {
  327. sortChanged = true;
  328. repaint();
  329. triggerAsyncUpdate();
  330. }
  331. //==============================================================================
  332. String TableHeaderComponent::toString() const
  333. {
  334. String s;
  335. XmlElement doc ("TABLELAYOUT");
  336. doc.setAttribute ("sortedCol", getSortColumnId());
  337. doc.setAttribute ("sortForwards", isSortedForwards());
  338. for (auto* ci : columns)
  339. {
  340. auto* e = doc.createNewChildElement ("COLUMN");
  341. e->setAttribute ("id", ci->id);
  342. e->setAttribute ("visible", ci->isVisible());
  343. e->setAttribute ("width", ci->width);
  344. }
  345. return doc.toString (XmlElement::TextFormat().singleLine().withoutHeader());
  346. }
  347. void TableHeaderComponent::restoreFromString (const String& storedVersion)
  348. {
  349. if (auto storedXML = parseXMLIfTagMatches (storedVersion, "TABLELAYOUT"))
  350. {
  351. int index = 0;
  352. for (auto* col : storedXML->getChildIterator())
  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* newListener)
  371. {
  372. listeners.addIfNotAlreadyThere (newListener);
  373. }
  374. void TableHeaderComponent::removeListener (Listener* 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. auto& lf = getLookAndFeel();
  401. lf.drawTableHeaderBackground (g, *this);
  402. auto 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.reset();
  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. dragOverlayComp.reset (new DragOverlayComp (createComponentSnapshot (columnRect, false, 2.0f)));
  563. addAndMakeVisible (dragOverlayComp.get());
  564. columnIdBeingDragged = temp;
  565. dragOverlayComp->setBounds (columnRect);
  566. for (int i = listeners.size(); --i >= 0;)
  567. {
  568. listeners.getUnchecked(i)->tableColumnDraggingChanged (this, columnIdBeingDragged);
  569. i = jmin (i, listeners.size() - 1);
  570. }
  571. }
  572. }
  573. }
  574. void TableHeaderComponent::endDrag (const int finalIndex)
  575. {
  576. if (columnIdBeingDragged != 0)
  577. {
  578. moveColumn (columnIdBeingDragged, finalIndex);
  579. columnIdBeingDragged = 0;
  580. repaint();
  581. for (int i = listeners.size(); --i >= 0;)
  582. {
  583. listeners.getUnchecked(i)->tableColumnDraggingChanged (this, 0);
  584. i = jmin (i, listeners.size() - 1);
  585. }
  586. }
  587. }
  588. void TableHeaderComponent::mouseUp (const MouseEvent& e)
  589. {
  590. mouseDrag (e);
  591. for (auto* c : columns)
  592. if (c->isVisible())
  593. c->lastDeliberateWidth = c->width;
  594. columnIdBeingResized = 0;
  595. repaint();
  596. endDrag (getIndexOfColumnId (columnIdBeingDragged, true));
  597. updateColumnUnderMouse (e);
  598. if (columnIdUnderMouse != 0 && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  599. columnClicked (columnIdUnderMouse, e.mods);
  600. dragOverlayComp.reset();
  601. }
  602. MouseCursor TableHeaderComponent::getMouseCursor()
  603. {
  604. if (columnIdBeingResized != 0 || (getResizeDraggerAt (getMouseXYRelative().getX()) != 0 && ! isMouseButtonDown()))
  605. return MouseCursor (MouseCursor::LeftRightResizeCursor);
  606. return Component::getMouseCursor();
  607. }
  608. //==============================================================================
  609. bool TableHeaderComponent::ColumnInfo::isVisible() const
  610. {
  611. return (propertyFlags & TableHeaderComponent::visible) != 0;
  612. }
  613. TableHeaderComponent::ColumnInfo* TableHeaderComponent::getInfoForId (int id) const
  614. {
  615. for (auto* c : columns)
  616. if (c->id == id)
  617. return c;
  618. return nullptr;
  619. }
  620. int TableHeaderComponent::visibleIndexToTotalIndex (const int visibleIndex) const
  621. {
  622. int n = 0;
  623. for (int i = 0; i < columns.size(); ++i)
  624. {
  625. if (columns.getUnchecked(i)->isVisible())
  626. {
  627. if (n == visibleIndex)
  628. return i;
  629. ++n;
  630. }
  631. }
  632. return -1;
  633. }
  634. void TableHeaderComponent::sendColumnsChanged()
  635. {
  636. if (stretchToFit && lastDeliberateWidth > 0)
  637. resizeAllColumnsToFit (lastDeliberateWidth);
  638. repaint();
  639. columnsChanged = true;
  640. triggerAsyncUpdate();
  641. }
  642. void TableHeaderComponent::handleAsyncUpdate()
  643. {
  644. const bool changed = columnsChanged || sortChanged;
  645. const bool sized = columnsResized || changed;
  646. const bool sorted = sortChanged;
  647. columnsChanged = false;
  648. columnsResized = false;
  649. sortChanged = false;
  650. if (sorted)
  651. {
  652. for (int i = listeners.size(); --i >= 0;)
  653. {
  654. listeners.getUnchecked(i)->tableSortOrderChanged (this);
  655. i = jmin (i, listeners.size() - 1);
  656. }
  657. }
  658. if (changed)
  659. {
  660. for (int i = listeners.size(); --i >= 0;)
  661. {
  662. listeners.getUnchecked(i)->tableColumnsChanged (this);
  663. i = jmin (i, listeners.size() - 1);
  664. }
  665. }
  666. if (sized)
  667. {
  668. for (int i = listeners.size(); --i >= 0;)
  669. {
  670. listeners.getUnchecked(i)->tableColumnsResized (this);
  671. i = jmin (i, listeners.size() - 1);
  672. }
  673. }
  674. }
  675. int TableHeaderComponent::getResizeDraggerAt (const int mouseX) const
  676. {
  677. if (isPositiveAndBelow (mouseX, getWidth()))
  678. {
  679. const int draggableDistance = 3;
  680. int x = 0;
  681. for (auto* ci : columns)
  682. {
  683. if (ci->isVisible())
  684. {
  685. if (std::abs (mouseX - (x + ci->width)) <= draggableDistance
  686. && (ci->propertyFlags & resizable) != 0)
  687. return ci->id;
  688. x += ci->width;
  689. }
  690. }
  691. }
  692. return 0;
  693. }
  694. void TableHeaderComponent::setColumnUnderMouse (const int newCol)
  695. {
  696. if (newCol != columnIdUnderMouse)
  697. {
  698. columnIdUnderMouse = newCol;
  699. repaint();
  700. }
  701. }
  702. void TableHeaderComponent::updateColumnUnderMouse (const MouseEvent& e)
  703. {
  704. setColumnUnderMouse (reallyContains (e.getPosition(), true) && getResizeDraggerAt (e.x) == 0
  705. ? getColumnIdAtX (e.x) : 0);
  706. }
  707. static void tableHeaderMenuCallback (int result, TableHeaderComponent* tableHeader, int columnIdClicked)
  708. {
  709. if (tableHeader != nullptr && result != 0)
  710. tableHeader->reactToMenuItem (result, columnIdClicked);
  711. }
  712. void TableHeaderComponent::showColumnChooserMenu (const int columnIdClicked)
  713. {
  714. PopupMenu m;
  715. addMenuItems (m, columnIdClicked);
  716. if (m.getNumItems() > 0)
  717. {
  718. m.setLookAndFeel (&getLookAndFeel());
  719. m.showMenuAsync (PopupMenu::Options(),
  720. ModalCallbackFunction::forComponent (tableHeaderMenuCallback, this, columnIdClicked));
  721. }
  722. }
  723. void TableHeaderComponent::Listener::tableColumnDraggingChanged (TableHeaderComponent*, int)
  724. {
  725. }
  726. //==============================================================================
  727. std::unique_ptr<AccessibilityHandler> TableHeaderComponent::createAccessibilityHandler()
  728. {
  729. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::tableHeader);
  730. }
  731. } // namespace juce