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.

219 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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
  19. {
  20. struct CustomMouseCursorInfo
  21. {
  22. CustomMouseCursorInfo (const Image& im, Point<int> hs, float scale = 1.0f) noexcept
  23. : image (im), hotspot (hs), scaleFactor (scale)
  24. {}
  25. void* create() const;
  26. Image image;
  27. const Point<int> hotspot;
  28. const float scaleFactor;
  29. JUCE_DECLARE_NON_COPYABLE (CustomMouseCursorInfo)
  30. };
  31. class MouseCursor::SharedCursorHandle
  32. {
  33. public:
  34. explicit SharedCursorHandle (const MouseCursor::StandardCursorType type)
  35. : handle (createStandardMouseCursor (type)),
  36. standardType (type),
  37. isStandard (true)
  38. {
  39. }
  40. SharedCursorHandle (const Image& image, Point<int> hotSpot, float scaleFactor)
  41. : info (new CustomMouseCursorInfo (image, hotSpot, scaleFactor)),
  42. handle (info->create()),
  43. standardType (MouseCursor::NormalCursor),
  44. isStandard (false)
  45. {
  46. // your hotspot needs to be within the bounds of the image!
  47. jassert (image.getBounds().contains (hotSpot));
  48. }
  49. ~SharedCursorHandle()
  50. {
  51. deleteMouseCursor (handle, isStandard);
  52. }
  53. static SharedCursorHandle* createStandard (const MouseCursor::StandardCursorType type)
  54. {
  55. jassert (isPositiveAndBelow (type, MouseCursor::NumStandardCursorTypes));
  56. const SpinLock::ScopedLockType sl (lock);
  57. auto& c = getSharedCursor (type);
  58. if (c == nullptr)
  59. c = new SharedCursorHandle (type);
  60. else
  61. c->retain();
  62. return c;
  63. }
  64. bool isStandardType (MouseCursor::StandardCursorType type) const noexcept
  65. {
  66. return type == standardType && isStandard;
  67. }
  68. SharedCursorHandle* retain() noexcept
  69. {
  70. ++refCount;
  71. return this;
  72. }
  73. void release()
  74. {
  75. if (--refCount == 0)
  76. {
  77. if (isStandard)
  78. {
  79. const SpinLock::ScopedLockType sl (lock);
  80. getSharedCursor (standardType) = nullptr;
  81. }
  82. delete this;
  83. }
  84. }
  85. void* getHandle() const noexcept { return handle; }
  86. void setHandle (void* newHandle) { handle = newHandle; }
  87. MouseCursor::StandardCursorType getType() const noexcept { return standardType; }
  88. CustomMouseCursorInfo* getCustomInfo() const noexcept { return info.get(); }
  89. private:
  90. std::unique_ptr<CustomMouseCursorInfo> info;
  91. void* handle;
  92. Atomic<int> refCount { 1 };
  93. const MouseCursor::StandardCursorType standardType;
  94. const bool isStandard;
  95. static SpinLock lock;
  96. static SharedCursorHandle*& getSharedCursor (const MouseCursor::StandardCursorType type)
  97. {
  98. static SharedCursorHandle* cursors[MouseCursor::NumStandardCursorTypes] = {};
  99. return cursors[type];
  100. }
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedCursorHandle)
  102. };
  103. SpinLock MouseCursor::SharedCursorHandle::lock;
  104. //==============================================================================
  105. MouseCursor::MouseCursor() noexcept
  106. {
  107. }
  108. MouseCursor::MouseCursor (const StandardCursorType type)
  109. : cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : nullptr)
  110. {
  111. }
  112. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY)
  113. : MouseCursor (image, hotSpotX, hotSpotY, 1.0f)
  114. {
  115. }
  116. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY, float scaleFactor)
  117. : cursorHandle (new SharedCursorHandle (image, { hotSpotX, hotSpotY }, scaleFactor))
  118. {
  119. }
  120. MouseCursor::MouseCursor (const MouseCursor& other)
  121. : cursorHandle (other.cursorHandle == nullptr ? nullptr : other.cursorHandle->retain())
  122. {
  123. }
  124. MouseCursor::~MouseCursor()
  125. {
  126. if (cursorHandle != nullptr)
  127. cursorHandle->release();
  128. }
  129. MouseCursor& MouseCursor::operator= (const MouseCursor& other)
  130. {
  131. if (other.cursorHandle != nullptr)
  132. other.cursorHandle->retain();
  133. if (cursorHandle != nullptr)
  134. cursorHandle->release();
  135. cursorHandle = other.cursorHandle;
  136. return *this;
  137. }
  138. MouseCursor::MouseCursor (MouseCursor&& other) noexcept
  139. : cursorHandle (other.cursorHandle)
  140. {
  141. other.cursorHandle = nullptr;
  142. }
  143. MouseCursor& MouseCursor::operator= (MouseCursor&& other) noexcept
  144. {
  145. std::swap (cursorHandle, other.cursorHandle);
  146. return *this;
  147. }
  148. bool MouseCursor::operator== (const MouseCursor& other) const noexcept
  149. {
  150. return getHandle() == other.getHandle();
  151. }
  152. bool MouseCursor::operator== (StandardCursorType type) const noexcept
  153. {
  154. return cursorHandle != nullptr ? cursorHandle->isStandardType (type)
  155. : (type == NormalCursor);
  156. }
  157. bool MouseCursor::operator!= (const MouseCursor& other) const noexcept { return ! operator== (other); }
  158. bool MouseCursor::operator!= (StandardCursorType type) const noexcept { return ! operator== (type); }
  159. void* MouseCursor::getHandle() const noexcept
  160. {
  161. return cursorHandle != nullptr ? cursorHandle->getHandle() : nullptr;
  162. }
  163. void MouseCursor::showWaitCursor()
  164. {
  165. Desktop::getInstance().getMainMouseSource().showMouseCursor (MouseCursor::WaitCursor);
  166. }
  167. void MouseCursor::hideWaitCursor()
  168. {
  169. Desktop::getInstance().getMainMouseSource().revealCursor();
  170. }
  171. } // namespace juce