The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

951 lines
28KB

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