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.

221 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. struct CustomMouseCursorInfo
  22. {
  23. CustomMouseCursorInfo (const Image& im, int hsX, int hsY) noexcept
  24. : image (im), hotspot (hsX, hsY), scaleFactor (1.0f)
  25. {}
  26. CustomMouseCursorInfo (const Image& im, Point<int> hs, float scale) noexcept
  27. : image (im), hotspot (hs), scaleFactor (scale)
  28. {}
  29. void* create() const;
  30. Image image;
  31. const Point<int> hotspot;
  32. float scaleFactor;
  33. private:
  34. JUCE_DECLARE_NON_COPYABLE (CustomMouseCursorInfo)
  35. };
  36. class MouseCursor::SharedCursorHandle
  37. {
  38. public:
  39. explicit SharedCursorHandle (const MouseCursor::StandardCursorType type)
  40. : handle (createStandardMouseCursor (type)),
  41. refCount (1),
  42. standardType (type),
  43. isStandard (true)
  44. {
  45. }
  46. SharedCursorHandle (const Image& image, Point<int> hotSpot, const float scaleFactor)
  47. : handle (CustomMouseCursorInfo (image, hotSpot, scaleFactor).create()),
  48. refCount (1),
  49. standardType (MouseCursor::NormalCursor),
  50. isStandard (false)
  51. {
  52. }
  53. ~SharedCursorHandle()
  54. {
  55. deleteMouseCursor (handle, isStandard);
  56. }
  57. static SharedCursorHandle* createStandard (const MouseCursor::StandardCursorType type)
  58. {
  59. jassert (isPositiveAndBelow (type, MouseCursor::NumStandardCursorTypes));
  60. const SpinLock::ScopedLockType sl (lock);
  61. SharedCursorHandle*& c = getSharedCursor (type);
  62. if (c == nullptr)
  63. c = new SharedCursorHandle (type);
  64. else
  65. c->retain();
  66. return c;
  67. }
  68. bool isStandardType (MouseCursor::StandardCursorType type) const noexcept
  69. {
  70. return type == standardType && isStandard;
  71. }
  72. SharedCursorHandle* retain() noexcept
  73. {
  74. ++refCount;
  75. return this;
  76. }
  77. void release()
  78. {
  79. if (--refCount == 0)
  80. {
  81. if (isStandard)
  82. {
  83. const SpinLock::ScopedLockType sl (lock);
  84. getSharedCursor (standardType) = nullptr;
  85. }
  86. delete this;
  87. }
  88. }
  89. void* getHandle() const noexcept { return handle; }
  90. private:
  91. void* const handle;
  92. Atomic <int> refCount;
  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. : cursorHandle (nullptr)
  107. {
  108. }
  109. MouseCursor::MouseCursor (const StandardCursorType type)
  110. : cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : nullptr)
  111. {
  112. }
  113. MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotSpotY)
  114. : cursorHandle (new SharedCursorHandle (image, Point<int> (hotSpotX, hotSpotY), 1.0f))
  115. {
  116. }
  117. MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotSpotY, float scaleFactor)
  118. : cursorHandle (new SharedCursorHandle (image, Point<int> (hotSpotX, hotSpotY), scaleFactor))
  119. {
  120. }
  121. MouseCursor::MouseCursor (const MouseCursor& other)
  122. : cursorHandle (other.cursorHandle == nullptr ? nullptr : other.cursorHandle->retain())
  123. {
  124. }
  125. MouseCursor::~MouseCursor()
  126. {
  127. if (cursorHandle != nullptr)
  128. cursorHandle->release();
  129. }
  130. MouseCursor& MouseCursor::operator= (const MouseCursor& other)
  131. {
  132. if (other.cursorHandle != nullptr)
  133. other.cursorHandle->retain();
  134. if (cursorHandle != nullptr)
  135. cursorHandle->release();
  136. cursorHandle = other.cursorHandle;
  137. return *this;
  138. }
  139. MouseCursor::MouseCursor (MouseCursor&& other) noexcept
  140. : cursorHandle (other.cursorHandle)
  141. {
  142. other.cursorHandle = nullptr;
  143. }
  144. MouseCursor& MouseCursor::operator= (MouseCursor&& other) noexcept
  145. {
  146. std::swap (cursorHandle, other.cursorHandle);
  147. return *this;
  148. }
  149. bool MouseCursor::operator== (const MouseCursor& other) const noexcept
  150. {
  151. return getHandle() == other.getHandle();
  152. }
  153. bool MouseCursor::operator== (StandardCursorType type) const noexcept
  154. {
  155. return cursorHandle != nullptr ? cursorHandle->isStandardType (type)
  156. : (type == NormalCursor);
  157. }
  158. bool MouseCursor::operator!= (const MouseCursor& other) const noexcept { return ! operator== (other); }
  159. bool MouseCursor::operator!= (StandardCursorType type) const noexcept { return ! operator== (type); }
  160. void* MouseCursor::getHandle() const noexcept
  161. {
  162. return cursorHandle != nullptr ? cursorHandle->getHandle() : nullptr;
  163. }
  164. void MouseCursor::showWaitCursor()
  165. {
  166. Desktop::getInstance().getMainMouseSource().showMouseCursor (MouseCursor::WaitCursor);
  167. }
  168. void MouseCursor::hideWaitCursor()
  169. {
  170. Desktop::getInstance().getMainMouseSource().revealCursor();
  171. }
  172. } // namespace juce