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.

119 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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::detail
  19. {
  20. class ToolbarItemDragAndDropOverlayComponent : public Component
  21. {
  22. public:
  23. ToolbarItemDragAndDropOverlayComponent()
  24. : isDragging (false)
  25. {
  26. setAlwaysOnTop (true);
  27. setRepaintsOnMouseActivity (true);
  28. setMouseCursor (MouseCursor::DraggingHandCursor);
  29. }
  30. void paint (Graphics& g) override
  31. {
  32. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  33. {
  34. if (isMouseOverOrDragging()
  35. && tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  36. {
  37. g.setColour (findColour (Toolbar::editingModeOutlineColourId, true));
  38. g.drawRect (getLocalBounds(), jmin (2, (getWidth() - 1) / 2,
  39. (getHeight() - 1) / 2));
  40. }
  41. }
  42. }
  43. void mouseDown (const MouseEvent& e) override
  44. {
  45. isDragging = false;
  46. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  47. {
  48. tc->dragOffsetX = e.x;
  49. tc->dragOffsetY = e.y;
  50. }
  51. }
  52. void mouseDrag (const MouseEvent& e) override
  53. {
  54. if (e.mouseWasDraggedSinceMouseDown() && ! isDragging)
  55. {
  56. isDragging = true;
  57. if (DragAndDropContainer* const dnd = DragAndDropContainer::findParentDragContainerFor (this))
  58. {
  59. dnd->startDragging (Toolbar::toolbarDragDescriptor, getParentComponent(), ScaledImage(), true, nullptr, &e.source);
  60. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  61. {
  62. tc->isBeingDragged = true;
  63. if (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  64. tc->setVisible (false);
  65. }
  66. }
  67. }
  68. }
  69. void mouseUp (const MouseEvent&) override
  70. {
  71. isDragging = false;
  72. if (ToolbarItemComponent* const tc = getToolbarItemComponent())
  73. {
  74. tc->isBeingDragged = false;
  75. if (Toolbar* const tb = tc->getToolbar())
  76. tb->updateAllItemPositions (true);
  77. else if (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
  78. delete tc;
  79. }
  80. }
  81. void parentSizeChanged() override
  82. {
  83. setBounds (0, 0, getParentWidth(), getParentHeight());
  84. }
  85. private:
  86. //==============================================================================
  87. bool isDragging;
  88. ToolbarItemComponent* getToolbarItemComponent() const noexcept
  89. {
  90. return dynamic_cast<ToolbarItemComponent*> (getParentComponent());
  91. }
  92. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ToolbarItemDragAndDropOverlayComponent)
  93. };
  94. } // namespace juce::detail