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.

juce_MouseCursor.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. class MouseCursor::SharedCursorHandle
  21. {
  22. public:
  23. explicit SharedCursorHandle (const MouseCursor::StandardCursorType type)
  24. : handle (type),
  25. standardType (type),
  26. standard (true)
  27. {
  28. }
  29. SharedCursorHandle (const ScaledImage& image, Point<int> hotSpot)
  30. : info { image, hotSpot },
  31. handle (info),
  32. standardType (MouseCursor::NormalCursor),
  33. standard (false)
  34. {
  35. // your hotspot needs to be within the bounds of the image!
  36. jassert (image.getScaledBounds().toNearestInt().contains (hotSpot));
  37. }
  38. static std::shared_ptr<SharedCursorHandle> createStandard (const MouseCursor::StandardCursorType type)
  39. {
  40. if (! isPositiveAndBelow (type, MouseCursor::NumStandardCursorTypes))
  41. return nullptr;
  42. static SpinLock mutex;
  43. static std::array<std::weak_ptr<SharedCursorHandle>, MouseCursor::NumStandardCursorTypes> cursors;
  44. const SpinLock::ScopedLockType sl (mutex);
  45. auto& weak = cursors[type];
  46. if (auto strong = weak.lock())
  47. return strong;
  48. auto strong = std::make_shared<SharedCursorHandle> (type);
  49. weak = strong;
  50. return strong;
  51. }
  52. bool isStandardType (MouseCursor::StandardCursorType type) const noexcept
  53. {
  54. return type == standardType && standard;
  55. }
  56. PlatformSpecificHandle* getHandle() noexcept { return &handle; }
  57. MouseCursor::StandardCursorType getType() const noexcept { return standardType; }
  58. private:
  59. CustomMouseCursorInfo info;
  60. PlatformSpecificHandle handle;
  61. const MouseCursor::StandardCursorType standardType;
  62. const bool standard;
  63. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedCursorHandle)
  64. };
  65. //==============================================================================
  66. MouseCursor::MouseCursor() noexcept = default;
  67. MouseCursor::MouseCursor (const StandardCursorType type)
  68. : cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : nullptr)
  69. {
  70. }
  71. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY)
  72. : MouseCursor (ScaledImage (image), { hotSpotX, hotSpotY })
  73. {
  74. }
  75. MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY, float scaleFactor)
  76. : MouseCursor (ScaledImage (image, scaleFactor), { hotSpotX, hotSpotY })
  77. {
  78. }
  79. MouseCursor::MouseCursor (const ScaledImage& image, Point<int> hotSpot)
  80. : cursorHandle (std::make_shared<SharedCursorHandle> (image, hotSpot))
  81. {
  82. }
  83. MouseCursor::MouseCursor (const MouseCursor&) = default;
  84. MouseCursor::~MouseCursor() = default;
  85. MouseCursor& MouseCursor::operator= (const MouseCursor&) = default;
  86. MouseCursor::MouseCursor (MouseCursor&&) noexcept = default;
  87. MouseCursor& MouseCursor::operator= (MouseCursor&&) noexcept = default;
  88. bool MouseCursor::operator== (const MouseCursor& other) const noexcept
  89. {
  90. return getHandle() == other.getHandle();
  91. }
  92. bool MouseCursor::operator== (StandardCursorType type) const noexcept
  93. {
  94. return cursorHandle != nullptr ? cursorHandle->isStandardType (type)
  95. : (type == NormalCursor);
  96. }
  97. bool MouseCursor::operator!= (const MouseCursor& other) const noexcept { return ! operator== (other); }
  98. bool MouseCursor::operator!= (StandardCursorType type) const noexcept { return ! operator== (type); }
  99. void MouseCursor::showWaitCursor()
  100. {
  101. Desktop::getInstance().getMainMouseSource().showMouseCursor (MouseCursor::WaitCursor);
  102. }
  103. void MouseCursor::hideWaitCursor()
  104. {
  105. Desktop::getInstance().getMainMouseSource().revealCursor();
  106. }
  107. MouseCursor::PlatformSpecificHandle* MouseCursor::getHandle() const noexcept
  108. {
  109. return cursorHandle != nullptr ? cursorHandle->getHandle() : nullptr;
  110. }
  111. void MouseCursor::showInWindow (ComponentPeer* peer) const
  112. {
  113. PlatformSpecificHandle::showInWindow (getHandle(), peer);
  114. }
  115. } // namespace juce