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.

214 lines
5.9KB

  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. : handle (CustomMouseCursorInfo (image, hotSpot, scaleFactor).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. private:
  87. void* const handle;
  88. Atomic<int> refCount { 1 };
  89. const MouseCursor::StandardCursorType standardType;
  90. const bool isStandard;
  91. static SpinLock lock;
  92. static SharedCursorHandle*& getSharedCursor (const MouseCursor::StandardCursorType type)
  93. {
  94. static SharedCursorHandle* cursors[MouseCursor::NumStandardCursorTypes] = {};
  95. return cursors[type];
  96. }
  97. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedCursorHandle)
  98. };
  99. SpinLock MouseCursor::SharedCursorHandle::lock;
  100. //==============================================================================
  101. MouseCursor::MouseCursor() noexcept
  102. {
  103. }
  104. MouseCursor::MouseCursor (const StandardCursorType type)
  105. : cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : nullptr)
  106. {
  107. }
  108. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY)
  109. : MouseCursor (image, hotSpotX, hotSpotY, 1.0f)
  110. {
  111. }
  112. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY, float scaleFactor)
  113. : cursorHandle (new SharedCursorHandle (image, { hotSpotX, hotSpotY }, scaleFactor))
  114. {
  115. }
  116. MouseCursor::MouseCursor (const MouseCursor& other)
  117. : cursorHandle (other.cursorHandle == nullptr ? nullptr : other.cursorHandle->retain())
  118. {
  119. }
  120. MouseCursor::~MouseCursor()
  121. {
  122. if (cursorHandle != nullptr)
  123. cursorHandle->release();
  124. }
  125. MouseCursor& MouseCursor::operator= (const MouseCursor& other)
  126. {
  127. if (other.cursorHandle != nullptr)
  128. other.cursorHandle->retain();
  129. if (cursorHandle != nullptr)
  130. cursorHandle->release();
  131. cursorHandle = other.cursorHandle;
  132. return *this;
  133. }
  134. MouseCursor::MouseCursor (MouseCursor&& other) noexcept
  135. : cursorHandle (other.cursorHandle)
  136. {
  137. other.cursorHandle = nullptr;
  138. }
  139. MouseCursor& MouseCursor::operator= (MouseCursor&& other) noexcept
  140. {
  141. std::swap (cursorHandle, other.cursorHandle);
  142. return *this;
  143. }
  144. bool MouseCursor::operator== (const MouseCursor& other) const noexcept
  145. {
  146. return getHandle() == other.getHandle();
  147. }
  148. bool MouseCursor::operator== (StandardCursorType type) const noexcept
  149. {
  150. return cursorHandle != nullptr ? cursorHandle->isStandardType (type)
  151. : (type == NormalCursor);
  152. }
  153. bool MouseCursor::operator!= (const MouseCursor& other) const noexcept { return ! operator== (other); }
  154. bool MouseCursor::operator!= (StandardCursorType type) const noexcept { return ! operator== (type); }
  155. void* MouseCursor::getHandle() const noexcept
  156. {
  157. return cursorHandle != nullptr ? cursorHandle->getHandle() : nullptr;
  158. }
  159. void MouseCursor::showWaitCursor()
  160. {
  161. Desktop::getInstance().getMainMouseSource().showMouseCursor (MouseCursor::WaitCursor);
  162. }
  163. void MouseCursor::hideWaitCursor()
  164. {
  165. Desktop::getInstance().getMainMouseSource().revealCursor();
  166. }
  167. } // namespace juce