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.

930 lines
27KB

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