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.

155 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_MAC
  18. //==============================================================================
  19. namespace MouseCursorHelpers
  20. {
  21. NSImage* createNSImage (const Image&);
  22. NSImage* createNSImage (const Image& image)
  23. {
  24. JUCE_AUTORELEASEPOOL
  25. {
  26. NSImage* im = [[NSImage alloc] init];
  27. [im setSize: NSMakeSize (image.getWidth(), image.getHeight())];
  28. [im lockFocus];
  29. CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
  30. CGImageRef imageRef = juce_createCoreGraphicsImage (image, colourSpace, false);
  31. CGColorSpaceRelease (colourSpace);
  32. CGContextRef cg = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
  33. CGContextDrawImage (cg, convertToCGRect (image.getBounds()), imageRef);
  34. CGImageRelease (imageRef);
  35. [im unlockFocus];
  36. return im;
  37. }
  38. }
  39. static void* fromWebKitFile (const char* filename, float hx, float hy)
  40. {
  41. FileInputStream fileStream (String ("/System/Library/Frameworks/WebKit.framework/Frameworks/WebCore.framework/Resources/") + filename);
  42. BufferedInputStream buf (fileStream, 4096);
  43. PNGImageFormat pngFormat;
  44. Image im (pngFormat.decodeImage (buf));
  45. if (im.isValid())
  46. return CustomMouseCursorInfo (im, (int) (hx * im.getWidth()),
  47. (int) (hy * im.getHeight())).create();
  48. jassertfalse;
  49. return nullptr;
  50. }
  51. }
  52. void* CustomMouseCursorInfo::create() const
  53. {
  54. NSImage* im = MouseCursorHelpers::createNSImage (image);
  55. NSCursor* c = [[NSCursor alloc] initWithImage: im
  56. hotSpot: NSMakePoint (hotspot.x, hotspot.y)];
  57. [im release];
  58. return c;
  59. }
  60. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  61. {
  62. JUCE_AUTORELEASEPOOL
  63. {
  64. NSCursor* c = nil;
  65. switch (type)
  66. {
  67. case NormalCursor:
  68. case ParentCursor: c = [NSCursor arrowCursor]; break;
  69. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 8, 8, true), 0, 0).create();
  70. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  71. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  72. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  73. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  74. case LeftRightResizeCursor: c = [NSCursor resizeLeftRightCursor]; break;
  75. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  76. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  77. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  78. case CopyingCursor: return MouseCursorHelpers::fromWebKitFile ("copyCursor.png", 0, 0);
  79. case UpDownResizeCursor:
  80. case TopEdgeResizeCursor:
  81. case BottomEdgeResizeCursor:
  82. return MouseCursorHelpers::fromWebKitFile ("northSouthResizeCursor.png", 0.5f, 0.5f);
  83. case TopLeftCornerResizeCursor:
  84. case BottomRightCornerResizeCursor:
  85. return MouseCursorHelpers::fromWebKitFile ("northWestSouthEastResizeCursor.png", 0.5f, 0.5f);
  86. case TopRightCornerResizeCursor:
  87. case BottomLeftCornerResizeCursor:
  88. return MouseCursorHelpers::fromWebKitFile ("northEastSouthWestResizeCursor.png", 0.5f, 0.5f);
  89. case UpDownLeftRightResizeCursor:
  90. return MouseCursorHelpers::fromWebKitFile ("moveCursor.png", 0.5f, 0.5f);
  91. default:
  92. jassertfalse;
  93. break;
  94. }
  95. [c retain];
  96. return c;
  97. }
  98. }
  99. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  100. {
  101. [((NSCursor*) cursorHandle) release];
  102. }
  103. void MouseCursor::showInAllWindows() const
  104. {
  105. showInWindow (nullptr);
  106. }
  107. void MouseCursor::showInWindow (ComponentPeer*) const
  108. {
  109. NSCursor* c = (NSCursor*) getHandle();
  110. if (c == nil)
  111. c = [NSCursor arrowCursor];
  112. [c set];
  113. }
  114. #else
  115. void* CustomMouseCursorInfo::create() const { return nullptr; }
  116. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type) { return nullptr; }
  117. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool isStandard) {}
  118. void MouseCursor::showInAllWindows() const {}
  119. void MouseCursor::showInWindow (ComponentPeer*) const {}
  120. #endif