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.

188 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_MouseCursor.h"
  21. #include "../juce_Component.h"
  22. #include "../lookandfeel/juce_LookAndFeel.h"
  23. #include "../mouse/juce_MouseInputSource.h"
  24. #include "../../../threads/juce_ScopedLock.h"
  25. //==============================================================================
  26. class MouseCursor::SharedCursorHandle
  27. {
  28. public:
  29. explicit SharedCursorHandle (const MouseCursor::StandardCursorType type)
  30. : handle (createStandardMouseCursor (type)),
  31. refCount (1),
  32. standardType (type),
  33. isStandard (true)
  34. {
  35. }
  36. SharedCursorHandle (const Image& image, const int hotSpotX, const int hotSpotY)
  37. : handle (createMouseCursorFromImage (image, hotSpotX, hotSpotY)),
  38. refCount (1),
  39. standardType (MouseCursor::NormalCursor),
  40. isStandard (false)
  41. {
  42. }
  43. static SharedCursorHandle* createStandard (const MouseCursor::StandardCursorType type)
  44. {
  45. const ScopedLock sl (getLock());
  46. for (int i = getCursors().size(); --i >= 0;)
  47. {
  48. SharedCursorHandle* const sc = getCursors().getUnchecked(i);
  49. if (sc->standardType == type)
  50. return sc->retain();
  51. }
  52. SharedCursorHandle* const sc = new SharedCursorHandle (type);
  53. getCursors().add (sc);
  54. return sc;
  55. }
  56. SharedCursorHandle* retain() throw()
  57. {
  58. ++refCount;
  59. return this;
  60. }
  61. void release()
  62. {
  63. if (--refCount == 0)
  64. {
  65. if (isStandard)
  66. {
  67. const ScopedLock sl (getLock());
  68. getCursors().removeValue (this);
  69. }
  70. delete this;
  71. }
  72. }
  73. void* getHandle() const throw() { return handle; }
  74. //==============================================================================
  75. juce_UseDebuggingNewOperator
  76. private:
  77. void* const handle;
  78. Atomic <int> refCount;
  79. const MouseCursor::StandardCursorType standardType;
  80. const bool isStandard;
  81. static CriticalSection& getLock()
  82. {
  83. static CriticalSection lock;
  84. return lock;
  85. }
  86. static Array <SharedCursorHandle*>& getCursors()
  87. {
  88. static Array <SharedCursorHandle*> cursors;
  89. return cursors;
  90. }
  91. ~SharedCursorHandle()
  92. {
  93. deleteMouseCursor (handle, isStandard);
  94. }
  95. SharedCursorHandle& operator= (const SharedCursorHandle&);
  96. };
  97. //==============================================================================
  98. MouseCursor::MouseCursor()
  99. : cursorHandle (SharedCursorHandle::createStandard (NormalCursor))
  100. {
  101. jassert (cursorHandle != 0);
  102. }
  103. MouseCursor::MouseCursor (const StandardCursorType type)
  104. : cursorHandle (SharedCursorHandle::createStandard (type))
  105. {
  106. jassert (cursorHandle != 0);
  107. }
  108. MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotSpotY)
  109. : cursorHandle (new SharedCursorHandle (image, hotSpotX, hotSpotY))
  110. {
  111. }
  112. MouseCursor::MouseCursor (const MouseCursor& other)
  113. : cursorHandle (other.cursorHandle->retain())
  114. {
  115. }
  116. MouseCursor::~MouseCursor()
  117. {
  118. cursorHandle->release();
  119. }
  120. MouseCursor& MouseCursor::operator= (const MouseCursor& other)
  121. {
  122. other.cursorHandle->retain();
  123. cursorHandle->release();
  124. cursorHandle = other.cursorHandle;
  125. return *this;
  126. }
  127. bool MouseCursor::operator== (const MouseCursor& other) const throw()
  128. {
  129. return getHandle() == other.getHandle();
  130. }
  131. bool MouseCursor::operator!= (const MouseCursor& other) const throw()
  132. {
  133. return getHandle() != other.getHandle();
  134. }
  135. void* MouseCursor::getHandle() const throw()
  136. {
  137. return cursorHandle->getHandle();
  138. }
  139. void MouseCursor::showWaitCursor()
  140. {
  141. Desktop::getInstance().getMainMouseSource().showMouseCursor (MouseCursor::WaitCursor);
  142. }
  143. void MouseCursor::hideWaitCursor()
  144. {
  145. Desktop::getInstance().getMainMouseSource().revealCursor();
  146. }
  147. END_JUCE_NAMESPACE