Audio plugin host https://kx.studio/carla
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.

juce_TableHeaderComponent.cpp 26KB

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