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 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class TableHeaderComponent::DragOverlayComp : public Component
  18. {
  19. public:
  20. DragOverlayComp (const Image& image_)
  21. : image (image_)
  22. {
  23. image.duplicateIfShared();
  24. image.multiplyAllAlphas (0.8f);
  25. setAlwaysOnTop (true);
  26. }
  27. void paint (Graphics& g) override
  28. {
  29. g.drawImageAt (image, 0, 0);
  30. }
  31. private:
  32. Image image;
  33. JUCE_DECLARE_NON_COPYABLE (DragOverlayComp)
  34. };
  35. //==============================================================================
  36. TableHeaderComponent::TableHeaderComponent()
  37. : columnsChanged (false),
  38. columnsResized (false),
  39. sortChanged (false),
  40. menuActive (true),
  41. stretchToFit (false),
  42. columnIdBeingResized (0),
  43. columnIdBeingDragged (0),
  44. columnIdUnderMouse (0),
  45. lastDeliberateWidth (0)
  46. {
  47. }
  48. TableHeaderComponent::~TableHeaderComponent()
  49. {
  50. dragOverlayComp = nullptr;
  51. }
  52. //==============================================================================
  53. void TableHeaderComponent::setPopupMenuActive (const bool hasMenu)
  54. {
  55. menuActive = hasMenu;
  56. }
  57. bool TableHeaderComponent::isPopupMenuActive() const { return menuActive; }
  58. //==============================================================================
  59. int TableHeaderComponent::getNumColumns (const bool onlyCountVisibleColumns) const
  60. {
  61. if (onlyCountVisibleColumns)
  62. {
  63. int num = 0;
  64. for (int i = columns.size(); --i >= 0;)
  65. if (columns.getUnchecked(i)->isVisible())
  66. ++num;
  67. return num;
  68. }
  69. return columns.size();
  70. }
  71. String TableHeaderComponent::getColumnName (const int columnId) const
  72. {
  73. if (const ColumnInfo* const ci = getInfoForId (columnId))
  74. return ci->name;
  75. return String::empty;
  76. }
  77. void TableHeaderComponent::setColumnName (const int columnId, const String& newName)
  78. {
  79. if (ColumnInfo* const ci = getInfoForId (columnId))
  80. {
  81. if (ci->name != newName)
  82. {
  83. ci->name = newName;
  84. sendColumnsChanged();
  85. }
  86. }
  87. }
  88. void TableHeaderComponent::addColumn (const String& columnName,
  89. const int columnId,
  90. const int width,
  91. const int minimumWidth,
  92. const int maximumWidth,
  93. const int propertyFlags,
  94. const int insertIndex)
  95. {
  96. // can't have a duplicate or null ID!
  97. jassert (columnId != 0 && getIndexOfColumnId (columnId, false) < 0);
  98. jassert (width > 0);
  99. ColumnInfo* const ci = new ColumnInfo();
  100. ci->name = columnName;
  101. ci->id = columnId;
  102. ci->width = width;
  103. ci->lastDeliberateWidth = width;
  104. ci->minimumWidth = minimumWidth;
  105. ci->maximumWidth = maximumWidth;
  106. if (ci->maximumWidth < 0)
  107. ci->maximumWidth = std::numeric_limits<int>::max();
  108. jassert (ci->maximumWidth >= ci->minimumWidth);
  109. ci->propertyFlags = propertyFlags;
  110. columns.insert (insertIndex, ci);
  111. sendColumnsChanged();
  112. }
  113. void TableHeaderComponent::removeColumn (const int columnIdToRemove)
  114. {
  115. const int index = getIndexOfColumnId (columnIdToRemove, false);
  116. if (index >= 0)
  117. {
  118. columns.remove (index);
  119. sortChanged = true;
  120. sendColumnsChanged();
  121. }
  122. }
  123. void TableHeaderComponent::removeAllColumns()
  124. {
  125. if (columns.size() > 0)
  126. {
  127. columns.clear();
  128. sendColumnsChanged();
  129. }
  130. }
  131. void TableHeaderComponent::moveColumn (const int columnId, int newIndex)
  132. {
  133. const int currentIndex = getIndexOfColumnId (columnId, false);
  134. newIndex = visibleIndexToTotalIndex (newIndex);
  135. if (columns [currentIndex] != 0 && currentIndex != newIndex)
  136. {
  137. columns.move (currentIndex, newIndex);
  138. sendColumnsChanged();
  139. }
  140. }
  141. int TableHeaderComponent::getColumnWidth (const int columnId) const
  142. {
  143. if (const ColumnInfo* const ci = getInfoForId (columnId))
  144. return ci->width;
  145. return 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. if (const ColumnInfo* const ci = columns [index])
  191. return ci->id;
  192. return 0;
  193. }
  194. Rectangle<int> TableHeaderComponent::getColumnPosition (const int index) const
  195. {
  196. int x = 0, width = 0, n = 0;
  197. for (int i = 0; i < columns.size(); ++i)
  198. {
  199. x += width;
  200. if (columns.getUnchecked(i)->isVisible())
  201. {
  202. width = columns.getUnchecked(i)->width;
  203. if (n++ == index)
  204. break;
  205. }
  206. else
  207. {
  208. width = 0;
  209. }
  210. }
  211. return Rectangle<int> (x, 0, width, getHeight());
  212. }
  213. int TableHeaderComponent::getColumnIdAtX (const int xToFind) const
  214. {
  215. if (xToFind >= 0)
  216. {
  217. int x = 0;
  218. for (int i = 0; i < columns.size(); ++i)
  219. {
  220. const ColumnInfo* const ci = columns.getUnchecked(i);
  221. if (ci->isVisible())
  222. {
  223. x += ci->width;
  224. if (xToFind < x)
  225. return ci->id;
  226. }
  227. }
  228. }
  229. return 0;
  230. }
  231. int TableHeaderComponent::getTotalWidth() const
  232. {
  233. int w = 0;
  234. for (int i = columns.size(); --i >= 0;)
  235. if (columns.getUnchecked(i)->isVisible())
  236. w += columns.getUnchecked(i)->width;
  237. return w;
  238. }
  239. void TableHeaderComponent::setStretchToFitActive (const bool shouldStretchToFit)
  240. {
  241. stretchToFit = shouldStretchToFit;
  242. lastDeliberateWidth = getTotalWidth();
  243. resized();
  244. }
  245. bool TableHeaderComponent::isStretchToFitActive() const
  246. {
  247. return stretchToFit;
  248. }
  249. void TableHeaderComponent::resizeAllColumnsToFit (int targetTotalWidth)
  250. {
  251. if (stretchToFit && getWidth() > 0
  252. && columnIdBeingResized == 0 && columnIdBeingDragged == 0)
  253. {
  254. lastDeliberateWidth = targetTotalWidth;
  255. resizeColumnsToFit (0, targetTotalWidth);
  256. }
  257. }
  258. void TableHeaderComponent::resizeColumnsToFit (int firstColumnIndex, int targetTotalWidth)
  259. {
  260. targetTotalWidth = jmax (targetTotalWidth, 0);
  261. StretchableObjectResizer sor;
  262. for (int i = firstColumnIndex; i < columns.size(); ++i)
  263. {
  264. ColumnInfo* const ci = columns.getUnchecked(i);
  265. if (ci->isVisible())
  266. sor.addItem (ci->lastDeliberateWidth, ci->minimumWidth, ci->maximumWidth);
  267. }
  268. sor.resizeToFit (targetTotalWidth);
  269. int visIndex = 0;
  270. for (int i = firstColumnIndex; i < columns.size(); ++i)
  271. {
  272. ColumnInfo* const ci = columns.getUnchecked(i);
  273. if (ci->isVisible())
  274. {
  275. const int newWidth = jlimit (ci->minimumWidth, ci->maximumWidth,
  276. (int) std::floor (sor.getItemSize (visIndex++)));
  277. if (newWidth != ci->width)
  278. {
  279. ci->width = newWidth;
  280. repaint();
  281. columnsResized = true;
  282. triggerAsyncUpdate();
  283. }
  284. }
  285. }
  286. }
  287. void TableHeaderComponent::setColumnVisible (const int columnId, const bool shouldBeVisible)
  288. {
  289. if (ColumnInfo* const ci = getInfoForId (columnId))
  290. {
  291. if (shouldBeVisible != ci->isVisible())
  292. {
  293. if (shouldBeVisible)
  294. ci->propertyFlags |= visible;
  295. else
  296. ci->propertyFlags &= ~visible;
  297. sendColumnsChanged();
  298. resized();
  299. }
  300. }
  301. }
  302. bool TableHeaderComponent::isColumnVisible (const int columnId) const
  303. {
  304. const ColumnInfo* const ci = getInfoForId (columnId);
  305. return ci != nullptr && ci->isVisible();
  306. }
  307. //==============================================================================
  308. void TableHeaderComponent::setSortColumnId (const int columnId, const bool sortForwards)
  309. {
  310. if (getSortColumnId() != columnId || isSortedForwards() != sortForwards)
  311. {
  312. for (int i = columns.size(); --i >= 0;)
  313. columns.getUnchecked(i)->propertyFlags &= ~(sortedForwards | sortedBackwards);
  314. if (ColumnInfo* const ci = getInfoForId (columnId))
  315. ci->propertyFlags |= (sortForwards ? sortedForwards : sortedBackwards);
  316. reSortTable();
  317. }
  318. }
  319. int TableHeaderComponent::getSortColumnId() const
  320. {
  321. for (int i = columns.size(); --i >= 0;)
  322. if ((columns.getUnchecked(i)->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
  323. return columns.getUnchecked(i)->id;
  324. return 0;
  325. }
  326. bool TableHeaderComponent::isSortedForwards() const
  327. {
  328. for (int i = columns.size(); --i >= 0;)
  329. if ((columns.getUnchecked(i)->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
  330. return (columns.getUnchecked(i)->propertyFlags & sortedForwards) != 0;
  331. return true;
  332. }
  333. void TableHeaderComponent::reSortTable()
  334. {
  335. sortChanged = true;
  336. repaint();
  337. triggerAsyncUpdate();
  338. }
  339. //==============================================================================
  340. String TableHeaderComponent::toString() const
  341. {
  342. String s;
  343. XmlElement doc ("TABLELAYOUT");
  344. doc.setAttribute ("sortedCol", getSortColumnId());
  345. doc.setAttribute ("sortForwards", isSortedForwards());
  346. for (int i = 0; i < columns.size(); ++i)
  347. {
  348. const ColumnInfo* const ci = columns.getUnchecked (i);
  349. XmlElement* const e = doc.createNewChildElement ("COLUMN");
  350. e->setAttribute ("id", ci->id);
  351. e->setAttribute ("visible", ci->isVisible());
  352. e->setAttribute ("width", ci->width);
  353. }
  354. return doc.createDocument ("", true, false);
  355. }
  356. void TableHeaderComponent::restoreFromString (const String& storedVersion)
  357. {
  358. ScopedPointer <XmlElement> storedXml (XmlDocument::parse (storedVersion));
  359. int index = 0;
  360. if (storedXml != nullptr && storedXml->hasTagName ("TABLELAYOUT"))
  361. {
  362. forEachXmlChildElement (*storedXml, col)
  363. {
  364. const int tabId = col->getIntAttribute ("id");
  365. if (ColumnInfo* const ci = getInfoForId (tabId))
  366. {
  367. columns.move (columns.indexOf (ci), index);
  368. ci->width = col->getIntAttribute ("width");
  369. setColumnVisible (tabId, col->getBoolAttribute ("visible"));
  370. }
  371. ++index;
  372. }
  373. columnsResized = true;
  374. sendColumnsChanged();
  375. setSortColumnId (storedXml->getIntAttribute ("sortedCol"),
  376. storedXml->getBoolAttribute ("sortForwards", true));
  377. }
  378. }
  379. //==============================================================================
  380. void TableHeaderComponent::addListener (Listener* const newListener)
  381. {
  382. listeners.addIfNotAlreadyThere (newListener);
  383. }
  384. void TableHeaderComponent::removeListener (Listener* const listenerToRemove)
  385. {
  386. listeners.removeFirstMatchingValue (listenerToRemove);
  387. }
  388. //==============================================================================
  389. void TableHeaderComponent::columnClicked (int columnId, const ModifierKeys& mods)
  390. {
  391. if (const ColumnInfo* const ci = getInfoForId (columnId))
  392. if ((ci->propertyFlags & sortable) != 0 && ! mods.isPopupMenu())
  393. setSortColumnId (columnId, (ci->propertyFlags & sortedForwards) == 0);
  394. }
  395. void TableHeaderComponent::addMenuItems (PopupMenu& menu, const int /*columnIdClicked*/)
  396. {
  397. for (int i = 0; i < columns.size(); ++i)
  398. {
  399. const ColumnInfo* const ci = columns.getUnchecked(i);
  400. if ((ci->propertyFlags & appearsOnColumnMenu) != 0)
  401. menu.addItem (ci->id, ci->name,
  402. (ci->propertyFlags & (sortedForwards | sortedBackwards)) == 0,
  403. isColumnVisible (ci->id));
  404. }
  405. }
  406. void TableHeaderComponent::reactToMenuItem (const int menuReturnId, const int /*columnIdClicked*/)
  407. {
  408. if (getIndexOfColumnId (menuReturnId, false) >= 0)
  409. setColumnVisible (menuReturnId, ! isColumnVisible (menuReturnId));
  410. }
  411. void TableHeaderComponent::paint (Graphics& g)
  412. {
  413. LookAndFeel& lf = getLookAndFeel();
  414. lf.drawTableHeaderBackground (g, *this);
  415. const Rectangle<int> clip (g.getClipBounds());
  416. int x = 0;
  417. for (int i = 0; i < columns.size(); ++i)
  418. {
  419. const ColumnInfo* const ci = columns.getUnchecked(i);
  420. if (ci->isVisible())
  421. {
  422. if (x + ci->width > clip.getX()
  423. && (ci->id != columnIdBeingDragged
  424. || dragOverlayComp == nullptr
  425. || ! dragOverlayComp->isVisible()))
  426. {
  427. Graphics::ScopedSaveState ss (g);
  428. g.setOrigin (x, 0);
  429. g.reduceClipRegion (0, 0, ci->width, getHeight());
  430. lf.drawTableHeaderColumn (g, ci->name, ci->id, ci->width, getHeight(),
  431. ci->id == columnIdUnderMouse,
  432. ci->id == columnIdUnderMouse && isMouseButtonDown(),
  433. ci->propertyFlags);
  434. }
  435. x += ci->width;
  436. if (x >= clip.getRight())
  437. break;
  438. }
  439. }
  440. }
  441. void TableHeaderComponent::resized()
  442. {
  443. }
  444. void TableHeaderComponent::mouseMove (const MouseEvent& e)
  445. {
  446. updateColumnUnderMouse (e);
  447. }
  448. void TableHeaderComponent::mouseEnter (const MouseEvent& e)
  449. {
  450. updateColumnUnderMouse (e);
  451. }
  452. void TableHeaderComponent::mouseExit (const MouseEvent&)
  453. {
  454. setColumnUnderMouse (0);
  455. }
  456. void TableHeaderComponent::mouseDown (const MouseEvent& e)
  457. {
  458. repaint();
  459. columnIdBeingResized = 0;
  460. columnIdBeingDragged = 0;
  461. if (columnIdUnderMouse != 0)
  462. {
  463. draggingColumnOffset = e.x - getColumnPosition (getIndexOfColumnId (columnIdUnderMouse, true)).getX();
  464. if (e.mods.isPopupMenu())
  465. columnClicked (columnIdUnderMouse, e.mods);
  466. }
  467. if (menuActive && e.mods.isPopupMenu())
  468. showColumnChooserMenu (columnIdUnderMouse);
  469. }
  470. void TableHeaderComponent::mouseDrag (const MouseEvent& e)
  471. {
  472. if (columnIdBeingResized == 0
  473. && columnIdBeingDragged == 0
  474. && ! (e.mouseWasClicked() || e.mods.isPopupMenu()))
  475. {
  476. dragOverlayComp = nullptr;
  477. columnIdBeingResized = getResizeDraggerAt (e.getMouseDownX());
  478. if (columnIdBeingResized != 0)
  479. {
  480. const ColumnInfo* const ci = getInfoForId (columnIdBeingResized);
  481. initialColumnWidth = ci->width;
  482. }
  483. else
  484. {
  485. beginDrag (e);
  486. }
  487. }
  488. if (columnIdBeingResized != 0)
  489. {
  490. if (const ColumnInfo* const ci = getInfoForId (columnIdBeingResized))
  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. }