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.

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