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

9 years ago
8 years ago
8 years ago
10 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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.mouseWasDraggedSinceMouseDown()
  475. && ! e.mods.isPopupMenu())
  476. {
  477. dragOverlayComp = nullptr;
  478. columnIdBeingResized = getResizeDraggerAt (e.getMouseDownX());
  479. if (columnIdBeingResized != 0)
  480. {
  481. const ColumnInfo* const ci = getInfoForId (columnIdBeingResized);
  482. jassert (ci != nullptr);
  483. initialColumnWidth = ci->width;
  484. }
  485. else
  486. {
  487. beginDrag (e);
  488. }
  489. }
  490. if (columnIdBeingResized != 0)
  491. {
  492. if (const ColumnInfo* const ci = getInfoForId (columnIdBeingResized))
  493. {
  494. int w = jlimit (ci->minimumWidth, ci->maximumWidth,
  495. initialColumnWidth + e.getDistanceFromDragStartX());
  496. if (stretchToFit)
  497. {
  498. // prevent us dragging a column too far right if we're in stretch-to-fit mode
  499. int minWidthOnRight = 0;
  500. for (int i = getIndexOfColumnId (columnIdBeingResized, false) + 1; i < columns.size(); ++i)
  501. if (columns.getUnchecked (i)->isVisible())
  502. minWidthOnRight += columns.getUnchecked (i)->minimumWidth;
  503. const Rectangle<int> currentPos (getColumnPosition (getIndexOfColumnId (columnIdBeingResized, true)));
  504. w = jmax (ci->minimumWidth, jmin (w, lastDeliberateWidth - minWidthOnRight - currentPos.getX()));
  505. }
  506. setColumnWidth (columnIdBeingResized, w);
  507. }
  508. }
  509. else if (columnIdBeingDragged != 0)
  510. {
  511. if (e.y >= -50 && e.y < getHeight() + 50)
  512. {
  513. if (dragOverlayComp != nullptr)
  514. {
  515. dragOverlayComp->setVisible (true);
  516. dragOverlayComp->setBounds (jlimit (0,
  517. jmax (0, getTotalWidth() - dragOverlayComp->getWidth()),
  518. e.x - draggingColumnOffset),
  519. 0,
  520. dragOverlayComp->getWidth(),
  521. getHeight());
  522. for (int i = columns.size(); --i >= 0;)
  523. {
  524. const int currentIndex = getIndexOfColumnId (columnIdBeingDragged, true);
  525. int newIndex = currentIndex;
  526. if (newIndex > 0)
  527. {
  528. // if the previous column isn't draggable, we can't move our column
  529. // past it, because that'd change the undraggable column's position..
  530. const ColumnInfo* const previous = columns.getUnchecked (newIndex - 1);
  531. if ((previous->propertyFlags & draggable) != 0)
  532. {
  533. const int leftOfPrevious = getColumnPosition (newIndex - 1).getX();
  534. const int rightOfCurrent = getColumnPosition (newIndex).getRight();
  535. if (abs (dragOverlayComp->getX() - leftOfPrevious)
  536. < abs (dragOverlayComp->getRight() - rightOfCurrent))
  537. {
  538. --newIndex;
  539. }
  540. }
  541. }
  542. if (newIndex < columns.size() - 1)
  543. {
  544. // if the next column isn't draggable, we can't move our column
  545. // past it, because that'd change the undraggable column's position..
  546. const ColumnInfo* const nextCol = columns.getUnchecked (newIndex + 1);
  547. if ((nextCol->propertyFlags & draggable) != 0)
  548. {
  549. const int leftOfCurrent = getColumnPosition (newIndex).getX();
  550. const int rightOfNext = getColumnPosition (newIndex + 1).getRight();
  551. if (abs (dragOverlayComp->getX() - leftOfCurrent)
  552. > abs (dragOverlayComp->getRight() - rightOfNext))
  553. {
  554. ++newIndex;
  555. }
  556. }
  557. }
  558. if (newIndex != currentIndex)
  559. moveColumn (columnIdBeingDragged, newIndex);
  560. else
  561. break;
  562. }
  563. }
  564. }
  565. else
  566. {
  567. endDrag (draggingColumnOriginalIndex);
  568. }
  569. }
  570. }
  571. void TableHeaderComponent::beginDrag (const MouseEvent& e)
  572. {
  573. if (columnIdBeingDragged == 0)
  574. {
  575. columnIdBeingDragged = getColumnIdAtX (e.getMouseDownX());
  576. const ColumnInfo* const ci = getInfoForId (columnIdBeingDragged);
  577. if (ci == nullptr || (ci->propertyFlags & draggable) == 0)
  578. {
  579. columnIdBeingDragged = 0;
  580. }
  581. else
  582. {
  583. draggingColumnOriginalIndex = getIndexOfColumnId (columnIdBeingDragged, true);
  584. const Rectangle<int> columnRect (getColumnPosition (draggingColumnOriginalIndex));
  585. const int temp = columnIdBeingDragged;
  586. columnIdBeingDragged = 0;
  587. addAndMakeVisible (dragOverlayComp = new DragOverlayComp (createComponentSnapshot (columnRect, false)));
  588. columnIdBeingDragged = temp;
  589. dragOverlayComp->setBounds (columnRect);
  590. for (int i = listeners.size(); --i >= 0;)
  591. {
  592. listeners.getUnchecked(i)->tableColumnDraggingChanged (this, columnIdBeingDragged);
  593. i = jmin (i, listeners.size() - 1);
  594. }
  595. }
  596. }
  597. }
  598. void TableHeaderComponent::endDrag (const int finalIndex)
  599. {
  600. if (columnIdBeingDragged != 0)
  601. {
  602. moveColumn (columnIdBeingDragged, finalIndex);
  603. columnIdBeingDragged = 0;
  604. repaint();
  605. for (int i = listeners.size(); --i >= 0;)
  606. {
  607. listeners.getUnchecked(i)->tableColumnDraggingChanged (this, 0);
  608. i = jmin (i, listeners.size() - 1);
  609. }
  610. }
  611. }
  612. void TableHeaderComponent::mouseUp (const MouseEvent& e)
  613. {
  614. mouseDrag (e);
  615. for (int i = columns.size(); --i >= 0;)
  616. if (columns.getUnchecked (i)->isVisible())
  617. columns.getUnchecked (i)->lastDeliberateWidth = columns.getUnchecked (i)->width;
  618. columnIdBeingResized = 0;
  619. repaint();
  620. endDrag (getIndexOfColumnId (columnIdBeingDragged, true));
  621. updateColumnUnderMouse (e);
  622. if (columnIdUnderMouse != 0 && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu()))
  623. columnClicked (columnIdUnderMouse, e.mods);
  624. dragOverlayComp = nullptr;
  625. }
  626. MouseCursor TableHeaderComponent::getMouseCursor()
  627. {
  628. if (columnIdBeingResized != 0 || (getResizeDraggerAt (getMouseXYRelative().getX()) != 0 && ! isMouseButtonDown()))
  629. return MouseCursor (MouseCursor::LeftRightResizeCursor);
  630. return Component::getMouseCursor();
  631. }
  632. //==============================================================================
  633. bool TableHeaderComponent::ColumnInfo::isVisible() const
  634. {
  635. return (propertyFlags & TableHeaderComponent::visible) != 0;
  636. }
  637. TableHeaderComponent::ColumnInfo* TableHeaderComponent::getInfoForId (const int id) const
  638. {
  639. for (int i = columns.size(); --i >= 0;)
  640. if (columns.getUnchecked(i)->id == id)
  641. return columns.getUnchecked(i);
  642. return nullptr;
  643. }
  644. int TableHeaderComponent::visibleIndexToTotalIndex (const int visibleIndex) const
  645. {
  646. int n = 0;
  647. for (int i = 0; i < columns.size(); ++i)
  648. {
  649. if (columns.getUnchecked(i)->isVisible())
  650. {
  651. if (n == visibleIndex)
  652. return i;
  653. ++n;
  654. }
  655. }
  656. return -1;
  657. }
  658. void TableHeaderComponent::sendColumnsChanged()
  659. {
  660. if (stretchToFit && lastDeliberateWidth > 0)
  661. resizeAllColumnsToFit (lastDeliberateWidth);
  662. repaint();
  663. columnsChanged = true;
  664. triggerAsyncUpdate();
  665. }
  666. void TableHeaderComponent::handleAsyncUpdate()
  667. {
  668. const bool changed = columnsChanged || sortChanged;
  669. const bool sized = columnsResized || changed;
  670. const bool sorted = sortChanged;
  671. columnsChanged = false;
  672. columnsResized = false;
  673. sortChanged = false;
  674. if (sorted)
  675. {
  676. for (int i = listeners.size(); --i >= 0;)
  677. {
  678. listeners.getUnchecked(i)->tableSortOrderChanged (this);
  679. i = jmin (i, listeners.size() - 1);
  680. }
  681. }
  682. if (changed)
  683. {
  684. for (int i = listeners.size(); --i >= 0;)
  685. {
  686. listeners.getUnchecked(i)->tableColumnsChanged (this);
  687. i = jmin (i, listeners.size() - 1);
  688. }
  689. }
  690. if (sized)
  691. {
  692. for (int i = listeners.size(); --i >= 0;)
  693. {
  694. listeners.getUnchecked(i)->tableColumnsResized (this);
  695. i = jmin (i, listeners.size() - 1);
  696. }
  697. }
  698. }
  699. int TableHeaderComponent::getResizeDraggerAt (const int mouseX) const
  700. {
  701. if (isPositiveAndBelow (mouseX, getWidth()))
  702. {
  703. const int draggableDistance = 3;
  704. int x = 0;
  705. for (int i = 0; i < columns.size(); ++i)
  706. {
  707. const ColumnInfo* const ci = columns.getUnchecked(i);
  708. if (ci->isVisible())
  709. {
  710. if (abs (mouseX - (x + ci->width)) <= draggableDistance
  711. && (ci->propertyFlags & resizable) != 0)
  712. return ci->id;
  713. x += ci->width;
  714. }
  715. }
  716. }
  717. return 0;
  718. }
  719. void TableHeaderComponent::setColumnUnderMouse (const int newCol)
  720. {
  721. if (newCol != columnIdUnderMouse)
  722. {
  723. columnIdUnderMouse = newCol;
  724. repaint();
  725. }
  726. }
  727. void TableHeaderComponent::updateColumnUnderMouse (const MouseEvent& e)
  728. {
  729. setColumnUnderMouse (reallyContains (e.getPosition(), true) && getResizeDraggerAt (e.x) == 0
  730. ? getColumnIdAtX (e.x) : 0);
  731. }
  732. static void tableHeaderMenuCallback (int result, TableHeaderComponent* tableHeader, int columnIdClicked)
  733. {
  734. if (tableHeader != nullptr && result != 0)
  735. tableHeader->reactToMenuItem (result, columnIdClicked);
  736. }
  737. void TableHeaderComponent::showColumnChooserMenu (const int columnIdClicked)
  738. {
  739. PopupMenu m;
  740. addMenuItems (m, columnIdClicked);
  741. if (m.getNumItems() > 0)
  742. {
  743. m.setLookAndFeel (&getLookAndFeel());
  744. m.showMenuAsync (PopupMenu::Options(),
  745. ModalCallbackFunction::forComponent (tableHeaderMenuCallback, this, columnIdClicked));
  746. }
  747. }
  748. void TableHeaderComponent::Listener::tableColumnDraggingChanged (TableHeaderComponent*, int)
  749. {
  750. }