juce_TableHeaderComponent.cpp 26 KB

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