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.

212 lines
6.0KB

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