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.

147 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. class MouseCursor::SharedCursorHandle
  16. {
  17. public:
  18. explicit SharedCursorHandle (const MouseCursor::StandardCursorType type)
  19. : handle (type),
  20. standardType (type),
  21. standard (true)
  22. {
  23. }
  24. SharedCursorHandle (const ScaledImage& image, Point<int> hotSpot)
  25. : info { image, hotSpot },
  26. handle (info),
  27. standardType (MouseCursor::NormalCursor),
  28. standard (false)
  29. {
  30. // your hotspot needs to be within the bounds of the image!
  31. jassert (image.getScaledBounds().toNearestInt().contains (hotSpot));
  32. }
  33. static std::shared_ptr<SharedCursorHandle> createStandard (const MouseCursor::StandardCursorType type)
  34. {
  35. if (! isPositiveAndBelow (type, MouseCursor::NumStandardCursorTypes))
  36. return nullptr;
  37. static SpinLock mutex;
  38. static std::array<std::weak_ptr<SharedCursorHandle>, MouseCursor::NumStandardCursorTypes> cursors;
  39. const SpinLock::ScopedLockType sl (mutex);
  40. auto& weak = cursors[type];
  41. if (auto strong = weak.lock())
  42. return strong;
  43. auto strong = std::make_shared<SharedCursorHandle> (type);
  44. weak = strong;
  45. return strong;
  46. }
  47. bool isStandardType (MouseCursor::StandardCursorType type) const noexcept
  48. {
  49. return type == standardType && standard;
  50. }
  51. PlatformSpecificHandle* getHandle() noexcept { return &handle; }
  52. MouseCursor::StandardCursorType getType() const noexcept { return standardType; }
  53. private:
  54. CustomMouseCursorInfo info;
  55. PlatformSpecificHandle handle;
  56. const MouseCursor::StandardCursorType standardType;
  57. const bool standard;
  58. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedCursorHandle)
  59. };
  60. //==============================================================================
  61. MouseCursor::MouseCursor() noexcept = default;
  62. MouseCursor::MouseCursor (const StandardCursorType type)
  63. : cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : nullptr)
  64. {
  65. }
  66. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY)
  67. : MouseCursor (ScaledImage (image), { hotSpotX, hotSpotY })
  68. {
  69. }
  70. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY, float scaleFactor)
  71. : MouseCursor (ScaledImage (image, scaleFactor), { hotSpotX, hotSpotY })
  72. {
  73. }
  74. MouseCursor::MouseCursor (const ScaledImage& image, Point<int> hotSpot)
  75. : cursorHandle (std::make_shared<SharedCursorHandle> (image, hotSpot))
  76. {
  77. }
  78. MouseCursor::MouseCursor (const MouseCursor&) = default;
  79. MouseCursor::~MouseCursor() = default;
  80. MouseCursor& MouseCursor::operator= (const MouseCursor&) = default;
  81. MouseCursor::MouseCursor (MouseCursor&&) noexcept = default;
  82. MouseCursor& MouseCursor::operator= (MouseCursor&&) noexcept = default;
  83. bool MouseCursor::operator== (const MouseCursor& other) const noexcept
  84. {
  85. return getHandle() == other.getHandle();
  86. }
  87. bool MouseCursor::operator== (StandardCursorType type) const noexcept
  88. {
  89. return cursorHandle != nullptr ? cursorHandle->isStandardType (type)
  90. : (type == NormalCursor);
  91. }
  92. bool MouseCursor::operator!= (const MouseCursor& other) const noexcept { return ! operator== (other); }
  93. bool MouseCursor::operator!= (StandardCursorType type) const noexcept { return ! operator== (type); }
  94. void MouseCursor::showWaitCursor()
  95. {
  96. Desktop::getInstance().getMainMouseSource().showMouseCursor (MouseCursor::WaitCursor);
  97. }
  98. void MouseCursor::hideWaitCursor()
  99. {
  100. Desktop::getInstance().getMainMouseSource().revealCursor();
  101. }
  102. MouseCursor::PlatformSpecificHandle* MouseCursor::getHandle() const noexcept
  103. {
  104. return cursorHandle != nullptr ? cursorHandle->getHandle() : nullptr;
  105. }
  106. void MouseCursor::showInWindow (ComponentPeer* peer) const
  107. {
  108. PlatformSpecificHandle::showInWindow (getHandle(), peer);
  109. }
  110. } // namespace juce