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.

904 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.reset();
  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] != nullptr && 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. if (auto storedXML = parseXML (storedVersion))
  351. {
  352. if (storedXML->hasTagName ("TABLELAYOUT"))
  353. {
  354. int index = 0;
  355. forEachXmlChildElement (*storedXML, col)
  356. {
  357. auto tabId = col->getIntAttribute ("id");
  358. if (auto* ci = getInfoForId (tabId))
  359. {
  360. columns.move (columns.indexOf (ci), index);
  361. ci->width = col->getIntAttribute ("width");
  362. setColumnVisible (tabId, col->getBoolAttribute ("visible"));
  363. }
  364. ++index;
  365. }
  366. columnsResized = true;
  367. sendColumnsChanged();
  368. setSortColumnId (storedXML->getIntAttribute ("sortedCol"),
  369. storedXML->getBoolAttribute ("sortForwards", true));
  370. }
  371. }
  372. }
  373. //==============================================================================
  374. void TableHeaderComponent::addListener (Listener* newListener)
  375. {
  376. listeners.addIfNotAlreadyThere (newListener);
  377. }
  378. void TableHeaderComponent::removeListener (Listener* listenerToRemove)
  379. {
  380. listeners.removeFirstMatchingValue (listenerToRemove);
  381. }
  382. //==============================================================================
  383. void TableHeaderComponent::columnClicked (int columnId, const ModifierKeys& mods)
  384. {
  385. if (auto* ci = getInfoForId (columnId))
  386. if ((ci->propertyFlags & sortable) != 0 && ! mods.isPopupMenu())
  387. setSortColumnId (columnId, (ci->propertyFlags & sortedForwards) == 0);
  388. }
  389. void TableHeaderComponent::addMenuItems (PopupMenu& menu, const int /*columnIdClicked*/)
  390. {
  391. for (auto* ci : columns)
  392. if ((ci->propertyFlags & appearsOnColumnMenu) != 0)
  393. menu.addItem (ci->id, ci->name,
  394. (ci->propertyFlags & (sortedForwards | sortedBackwards)) == 0,
  395. isColumnVisible (ci->id));
  396. }
  397. void TableHeaderComponent::reactToMenuItem (const int menuReturnId, const int /*columnIdClicked*/)
  398. {
  399. if (getIndexOfColumnId (menuReturnId, false) >= 0)
  400. setColumnVisible (menuReturnId, ! isColumnVisible (menuReturnId));
  401. }
  402. void TableHeaderComponent::paint (Graphics& g)
  403. {
  404. auto& lf = getLookAndFeel();
  405. lf.drawTableHeaderBackground (g, *this);
  406. auto clip = g.getClipBounds();
  407. int x = 0;
  408. for (auto* ci : columns)
  409. {
  410. if (ci->isVisible())
  411. {
  412. if (x + ci->width > clip.getX()
  413. && (ci->id != columnIdBeingDragged
  414. || dragOverlayComp == nullptr
  415. || ! dragOverlayComp->isVisible()))
  416. {
  417. Graphics::ScopedSaveState ss (g);
  418. g.setOrigin (x, 0);
  419. g.reduceClipRegion (0, 0, ci->width, getHeight());
  420. lf.drawTableHeaderColumn (g, *this, ci->name, ci->id, ci->width, getHeight(),
  421. ci->id == columnIdUnderMouse,
  422. ci->id == columnIdUnderMouse && isMouseButtonDown(),
  423. ci->propertyFlags);
  424. }
  425. x += ci->width;
  426. if (x >= clip.getRight())
  427. break;
  428. }
  429. }
  430. }
  431. void TableHeaderComponent::mouseMove (const MouseEvent& e) { updateColumnUnderMouse (e); }
  432. void TableHeaderComponent::mouseEnter (const MouseEvent& e) { updateColumnUnderMouse (e); }
  433. void TableHeaderComponent::mouseExit (const MouseEvent&) { setColumnUnderMouse (0); }
  434. void TableHeaderComponent::mouseDown (const MouseEvent& e)
  435. {
  436. repaint();
  437. columnIdBeingResized = 0;
  438. columnIdBeingDragged = 0;
  439. if (columnIdUnderMouse != 0)
  440. {
  441. draggingColumnOffset = e.x - getColumnPosition (getIndexOfColumnId (columnIdUnderMouse, true)).getX();
  442. if (e.mods.isPopupMenu())
  443. columnClicked (columnIdUnderMouse, e.mods);
  444. }
  445. if (menuActive && e.mods.isPopupMenu())
  446. showColumnChooserMenu (columnIdUnderMouse);
  447. }
  448. void TableHeaderComponent::mouseDrag (const MouseEvent& e)
  449. {
  450. if (columnIdBeingResized == 0
  451. && columnIdBeingDragged == 0
  452. && e.mouseWasDraggedSinceMouseDown()
  453. && ! e.mods.isPopupMenu())
  454. {
  455. dragOverlayComp.reset();
  456. columnIdBeingResized = getResizeDraggerAt (e.getMouseDownX());
  457. if (columnIdBeingResized != 0)
  458. {
  459. if (auto* ci = getInfoForId (columnIdBeingResized))
  460. initialColumnWidth = ci->width;
  461. else
  462. jassertfalse;
  463. }
  464. else
  465. {
  466. beginDrag (e);
  467. }
  468. }
  469. if (columnIdBeingResized != 0)
  470. {
  471. if (auto* ci = getInfoForId (columnIdBeingResized))
  472. {
  473. auto w = jlimit (ci->minimumWidth, ci->maximumWidth,
  474. initialColumnWidth + e.getDistanceFromDragStartX());
  475. if (stretchToFit)
  476. {
  477. // prevent us dragging a column too far right if we're in stretch-to-fit mode
  478. int minWidthOnRight = 0;
  479. for (int i = getIndexOfColumnId (columnIdBeingResized, false) + 1; i < columns.size(); ++i)
  480. if (columns.getUnchecked (i)->isVisible())
  481. minWidthOnRight += columns.getUnchecked (i)->minimumWidth;
  482. auto currentPos = getColumnPosition (getIndexOfColumnId (columnIdBeingResized, true));
  483. w = jmax (ci->minimumWidth, jmin (w, lastDeliberateWidth - minWidthOnRight - currentPos.getX()));
  484. }
  485. setColumnWidth (columnIdBeingResized, w);
  486. }
  487. }
  488. else if (columnIdBeingDragged != 0)
  489. {
  490. if (e.y >= -50 && e.y < getHeight() + 50)
  491. {
  492. if (dragOverlayComp != nullptr)
  493. {
  494. dragOverlayComp->setVisible (true);
  495. dragOverlayComp->setBounds (jlimit (0,
  496. jmax (0, getTotalWidth() - dragOverlayComp->getWidth()),
  497. e.x - draggingColumnOffset),
  498. 0,
  499. dragOverlayComp->getWidth(),
  500. getHeight());
  501. for (int i = columns.size(); --i >= 0;)
  502. {
  503. const int currentIndex = getIndexOfColumnId (columnIdBeingDragged, true);
  504. int newIndex = currentIndex;
  505. if (newIndex > 0)
  506. {
  507. // if the previous column isn't draggable, we can't move our column
  508. // past it, because that'd change the undraggable column's position..
  509. auto* previous = columns.getUnchecked (newIndex - 1);
  510. if ((previous->propertyFlags & draggable) != 0)
  511. {
  512. auto leftOfPrevious = getColumnPosition (newIndex - 1).getX();
  513. auto rightOfCurrent = getColumnPosition (newIndex).getRight();
  514. if (std::abs (dragOverlayComp->getX() - leftOfPrevious)
  515. < std::abs (dragOverlayComp->getRight() - rightOfCurrent))
  516. {
  517. --newIndex;
  518. }
  519. }
  520. }
  521. if (newIndex < columns.size() - 1)
  522. {
  523. // if the next column isn't draggable, we can't move our column
  524. // past it, because that'd change the undraggable column's position..
  525. auto* nextCol = columns.getUnchecked (newIndex + 1);
  526. if ((nextCol->propertyFlags & draggable) != 0)
  527. {
  528. auto leftOfCurrent = getColumnPosition (newIndex).getX();
  529. auto rightOfNext = getColumnPosition (newIndex + 1).getRight();
  530. if (std::abs (dragOverlayComp->getX() - leftOfCurrent)
  531. > std::abs (dragOverlayComp->getRight() - rightOfNext))
  532. {
  533. ++newIndex;
  534. }
  535. }
  536. }
  537. if (newIndex != currentIndex)
  538. moveColumn (columnIdBeingDragged, newIndex);
  539. else
  540. break;
  541. }
  542. }
  543. }
  544. else
  545. {
  546. endDrag (draggingColumnOriginalIndex);
  547. }
  548. }
  549. }
  550. void TableHeaderComponent::beginDrag (const MouseEvent& e)
  551. {
  552. if (columnIdBeingDragged == 0)
  553. {
  554. columnIdBeingDragged = getColumnIdAtX (e.getMouseDownX());
  555. auto* ci = getInfoForId (columnIdBeingDragged);
  556. if (ci == nullptr || (ci->propertyFlags & draggable) == 0)
  557. {
  558. columnIdBeingDragged = 0;
  559. }
  560. else
  561. {
  562. draggingColumnOriginalIndex = getIndexOfColumnId (columnIdBeingDragged, true);
  563. auto columnRect = getColumnPosition (draggingColumnOriginalIndex);
  564. auto temp = columnIdBeingDragged;
  565. columnIdBeingDragged = 0;
  566. dragOverlayComp.reset (new DragOverlayComp (createComponentSnapshot (columnRect, false)));
  567. addAndMakeVisible (dragOverlayComp.get());
  568. columnIdBeingDragged = temp;
  569. dragOverlayComp->setBounds (columnRect);
  570. for (int i = listeners.size(); --i >= 0;)
  571. {
  572. listeners.getUnchecked(i)->tableColumnDraggingChanged (this, columnIdBeingDragged);
  573. i = jmin (i, listeners.size() - 1);
  574. }
  575. }
  576. }
  577. }
  578. void TableHeaderComponent::endDrag (const int finalIndex)
  579. {
  580. if (columnIdBeingDragged != 0)
  581. {
  582. moveColumn (columnIdBeingDragged, finalIndex);
  583. columnIdBeingDragged = 0;
  584. repaint();
  585. for (int i = listeners.size(); --i >= 0;)
  586. {
  587. listeners.getUnchecked(i)->tableColumnDraggingChanged (this, 0);
  588. i = jmin (i, listeners.size() - 1);
  589. }
  590. }
  591. }
  592. void TableHeaderComponent::mouseUp (const MouseEvent& e)
  593. {
  594. mouseDrag (e);
  595. for (auto* c : columns)
  596. if (c->isVisible())
  597. c->lastDeliberateWidth = c->width;
  598. columnIdBeingResized = 0;
  599. repaint();
  600. endDrag (getIndexOfColumnId (columnIdBeingDragged, true));
  601. updateColumnUnderMouse (e);
  602. if (columnIdUnderMouse != 0 && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  603. columnClicked (columnIdUnderMouse, e.mods);
  604. dragOverlayComp.reset();
  605. }
  606. MouseCursor TableHeaderComponent::getMouseCursor()
  607. {
  608. if (columnIdBeingResized != 0 || (getResizeDraggerAt (getMouseXYRelative().getX()) != 0 && ! isMouseButtonDown()))
  609. return MouseCursor (MouseCursor::LeftRightResizeCursor);
  610. return Component::getMouseCursor();
  611. }
  612. //==============================================================================
  613. bool TableHeaderComponent::ColumnInfo::isVisible() const
  614. {
  615. return (propertyFlags & TableHeaderComponent::visible) != 0;
  616. }
  617. TableHeaderComponent::ColumnInfo* TableHeaderComponent::getInfoForId (int id) const
  618. {
  619. for (auto* c : columns)
  620. if (c->id == id)
  621. return c;
  622. return nullptr;
  623. }
  624. int TableHeaderComponent::visibleIndexToTotalIndex (const int visibleIndex) const
  625. {
  626. int n = 0;
  627. for (int i = 0; i < columns.size(); ++i)
  628. {
  629. if (columns.getUnchecked(i)->isVisible())
  630. {
  631. if (n == visibleIndex)
  632. return i;
  633. ++n;
  634. }
  635. }
  636. return -1;
  637. }
  638. void TableHeaderComponent::sendColumnsChanged()
  639. {
  640. if (stretchToFit && lastDeliberateWidth > 0)
  641. resizeAllColumnsToFit (lastDeliberateWidth);
  642. repaint();
  643. columnsChanged = true;
  644. triggerAsyncUpdate();
  645. }
  646. void TableHeaderComponent::handleAsyncUpdate()
  647. {
  648. const bool changed = columnsChanged || sortChanged;
  649. const bool sized = columnsResized || changed;
  650. const bool sorted = sortChanged;
  651. columnsChanged = false;
  652. columnsResized = false;
  653. sortChanged = false;
  654. if (sorted)
  655. {
  656. for (int i = listeners.size(); --i >= 0;)
  657. {
  658. listeners.getUnchecked(i)->tableSortOrderChanged (this);
  659. i = jmin (i, listeners.size() - 1);
  660. }
  661. }
  662. if (changed)
  663. {
  664. for (int i = listeners.size(); --i >= 0;)
  665. {
  666. listeners.getUnchecked(i)->tableColumnsChanged (this);
  667. i = jmin (i, listeners.size() - 1);
  668. }
  669. }
  670. if (sized)
  671. {
  672. for (int i = listeners.size(); --i >= 0;)
  673. {
  674. listeners.getUnchecked(i)->tableColumnsResized (this);
  675. i = jmin (i, listeners.size() - 1);
  676. }
  677. }
  678. }
  679. int TableHeaderComponent::getResizeDraggerAt (const int mouseX) const
  680. {
  681. if (isPositiveAndBelow (mouseX, getWidth()))
  682. {
  683. const int draggableDistance = 3;
  684. int x = 0;
  685. for (auto* ci : columns)
  686. {
  687. if (ci->isVisible())
  688. {
  689. if (std::abs (mouseX - (x + ci->width)) <= draggableDistance
  690. && (ci->propertyFlags & resizable) != 0)
  691. return ci->id;
  692. x += ci->width;
  693. }
  694. }
  695. }
  696. return 0;
  697. }
  698. void TableHeaderComponent::setColumnUnderMouse (const int newCol)
  699. {
  700. if (newCol != columnIdUnderMouse)
  701. {
  702. columnIdUnderMouse = newCol;
  703. repaint();
  704. }
  705. }
  706. void TableHeaderComponent::updateColumnUnderMouse (const MouseEvent& e)
  707. {
  708. setColumnUnderMouse (reallyContains (e.getPosition(), true) && getResizeDraggerAt (e.x) == 0
  709. ? getColumnIdAtX (e.x) : 0);
  710. }
  711. static void tableHeaderMenuCallback (int result, TableHeaderComponent* tableHeader, int columnIdClicked)
  712. {
  713. if (tableHeader != nullptr && result != 0)
  714. tableHeader->reactToMenuItem (result, columnIdClicked);
  715. }
  716. void TableHeaderComponent::showColumnChooserMenu (const int columnIdClicked)
  717. {
  718. PopupMenu m;
  719. addMenuItems (m, columnIdClicked);
  720. if (m.getNumItems() > 0)
  721. {
  722. m.setLookAndFeel (&getLookAndFeel());
  723. m.showMenuAsync (PopupMenu::Options(),
  724. ModalCallbackFunction::forComponent (tableHeaderMenuCallback, this, columnIdClicked));
  725. }
  726. }
  727. void TableHeaderComponent::Listener::tableColumnDraggingChanged (TableHeaderComponent*, int)
  728. {
  729. }
  730. } // namespace juce