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.

191 lines
5.2KB

  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 = 0; i < getCursors().size(); ++i)
  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 (0)
  100. {
  101. }
  102. MouseCursor::MouseCursor (const StandardCursorType type)
  103. : cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : 0)
  104. {
  105. }
  106. MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotSpotY)
  107. : cursorHandle (new SharedCursorHandle (image, hotSpotX, hotSpotY))
  108. {
  109. }
  110. MouseCursor::MouseCursor (const MouseCursor& other)
  111. : cursorHandle (other.cursorHandle == 0 ? 0 : other.cursorHandle->retain())
  112. {
  113. }
  114. MouseCursor::~MouseCursor()
  115. {
  116. if (cursorHandle != 0)
  117. cursorHandle->release();
  118. }
  119. MouseCursor& MouseCursor::operator= (const MouseCursor& other)
  120. {
  121. if (other.cursorHandle != 0)
  122. other.cursorHandle->retain();
  123. if (cursorHandle != 0)
  124. cursorHandle->release();
  125. cursorHandle = other.cursorHandle;
  126. return *this;
  127. }
  128. bool MouseCursor::operator== (const MouseCursor& other) const throw()
  129. {
  130. return getHandle() == other.getHandle();
  131. }
  132. bool MouseCursor::operator!= (const MouseCursor& other) const throw()
  133. {
  134. return getHandle() != other.getHandle();
  135. }
  136. void* MouseCursor::getHandle() const throw()
  137. {
  138. return cursorHandle != 0 ? cursorHandle->getHandle() : 0;
  139. }
  140. void MouseCursor::showWaitCursor()
  141. {
  142. Desktop::getInstance().getMainMouseSource().showMouseCursor (MouseCursor::WaitCursor);
  143. }
  144. void MouseCursor::hideWaitCursor()
  145. {
  146. Desktop::getInstance().getMainMouseSource().revealCursor();
  147. }
  148. END_JUCE_NAMESPACE