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.

220 lines
6.2KB

  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, Point<int> hs, float scale = 1.0f) noexcept
  24. : image (im), hotspot (hs), scaleFactor (scale)
  25. {}
  26. void* create() const;
  27. Image image;
  28. const Point<int> hotspot;
  29. const float scaleFactor;
  30. JUCE_DECLARE_NON_COPYABLE (CustomMouseCursorInfo)
  31. };
  32. class MouseCursor::SharedCursorHandle
  33. {
  34. public:
  35. explicit SharedCursorHandle (const MouseCursor::StandardCursorType type)
  36. : handle (createStandardMouseCursor (type)),
  37. standardType (type),
  38. isStandard (true)
  39. {
  40. }
  41. SharedCursorHandle (const Image& image, Point<int> hotSpot, float scaleFactor)
  42. : info (new CustomMouseCursorInfo (image, hotSpot, scaleFactor)),
  43. handle (info->create()),
  44. standardType (MouseCursor::NormalCursor),
  45. isStandard (false)
  46. {
  47. // your hotspot needs to be within the bounds of the image!
  48. jassert (image.getBounds().contains (hotSpot));
  49. }
  50. ~SharedCursorHandle()
  51. {
  52. deleteMouseCursor (handle, isStandard);
  53. }
  54. static SharedCursorHandle* createStandard (const MouseCursor::StandardCursorType type)
  55. {
  56. jassert (isPositiveAndBelow (type, MouseCursor::NumStandardCursorTypes));
  57. const SpinLock::ScopedLockType sl (lock);
  58. auto& c = getSharedCursor (type);
  59. if (c == nullptr)
  60. c = new SharedCursorHandle (type);
  61. else
  62. c->retain();
  63. return c;
  64. }
  65. bool isStandardType (MouseCursor::StandardCursorType type) const noexcept
  66. {
  67. return type == standardType && isStandard;
  68. }
  69. SharedCursorHandle* retain() noexcept
  70. {
  71. ++refCount;
  72. return this;
  73. }
  74. void release()
  75. {
  76. if (--refCount == 0)
  77. {
  78. if (isStandard)
  79. {
  80. const SpinLock::ScopedLockType sl (lock);
  81. getSharedCursor (standardType) = nullptr;
  82. }
  83. delete this;
  84. }
  85. }
  86. void* getHandle() const noexcept { return handle; }
  87. void setHandle (void* newHandle) { handle = newHandle; }
  88. MouseCursor::StandardCursorType getType() const noexcept { return standardType; }
  89. CustomMouseCursorInfo* getCustomInfo() const noexcept { return info.get(); }
  90. private:
  91. std::unique_ptr<CustomMouseCursorInfo> info;
  92. void* handle;
  93. Atomic<int> refCount { 1 };
  94. const MouseCursor::StandardCursorType standardType;
  95. const bool isStandard;
  96. static SpinLock lock;
  97. static SharedCursorHandle*& getSharedCursor (const MouseCursor::StandardCursorType type)
  98. {
  99. static SharedCursorHandle* cursors[MouseCursor::NumStandardCursorTypes] = {};
  100. return cursors[type];
  101. }
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedCursorHandle)
  103. };
  104. SpinLock MouseCursor::SharedCursorHandle::lock;
  105. //==============================================================================
  106. MouseCursor::MouseCursor() noexcept
  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, int hotSpotX, int hotSpotY)
  114. : MouseCursor (image, hotSpotX, hotSpotY, 1.0f)
  115. {
  116. }
  117. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY, float scaleFactor)
  118. : cursorHandle (new SharedCursorHandle (image, { 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