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.

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