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.

154 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& image)
  22. {
  23. JUCE_AUTORELEASEPOOL
  24. {
  25. NSImage* im = [[NSImage alloc] init];
  26. [im setSize: NSMakeSize (image.getWidth(), image.getHeight())];
  27. [im lockFocus];
  28. CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
  29. CGImageRef imageRef = juce_createCoreGraphicsImage (image, colourSpace, false);
  30. CGColorSpaceRelease (colourSpace);
  31. CGContextRef cg = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
  32. CGContextDrawImage (cg, convertToCGRect (image.getBounds()), imageRef);
  33. CGImageRelease (imageRef);
  34. [im unlockFocus];
  35. return im;
  36. }
  37. }
  38. static void* fromWebKitFile (const char* filename, float hx, float hy)
  39. {
  40. FileInputStream fileStream (String ("/System/Library/Frameworks/WebKit.framework/Frameworks/WebCore.framework/Resources/") + filename);
  41. BufferedInputStream buf (fileStream, 4096);
  42. PNGImageFormat pngFormat;
  43. Image im (pngFormat.decodeImage (buf));
  44. if (im.isValid())
  45. return CustomMouseCursorInfo (im, (int) (hx * im.getWidth()),
  46. (int) (hy * im.getHeight())).create();
  47. jassertfalse;
  48. return nullptr;
  49. }
  50. }
  51. void* CustomMouseCursorInfo::create() const
  52. {
  53. NSImage* im = MouseCursorHelpers::createNSImage (image);
  54. NSCursor* c = [[NSCursor alloc] initWithImage: im
  55. hotSpot: NSMakePoint (hotspot.x, hotspot.y)];
  56. [im release];
  57. return c;
  58. }
  59. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  60. {
  61. JUCE_AUTORELEASEPOOL
  62. {
  63. NSCursor* c = nil;
  64. switch (type)
  65. {
  66. case NormalCursor:
  67. case ParentCursor: c = [NSCursor arrowCursor]; break;
  68. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 8, 8, true), 0, 0).create();
  69. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  70. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  71. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  72. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  73. case LeftRightResizeCursor: c = [NSCursor resizeLeftRightCursor]; break;
  74. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  75. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  76. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  77. case CopyingCursor: return MouseCursorHelpers::fromWebKitFile ("copyCursor.png", 0, 0);
  78. case UpDownResizeCursor:
  79. case TopEdgeResizeCursor:
  80. case BottomEdgeResizeCursor:
  81. return MouseCursorHelpers::fromWebKitFile ("northSouthResizeCursor.png", 0.5f, 0.5f);
  82. case TopLeftCornerResizeCursor:
  83. case BottomRightCornerResizeCursor:
  84. return MouseCursorHelpers::fromWebKitFile ("northWestSouthEastResizeCursor.png", 0.5f, 0.5f);
  85. case TopRightCornerResizeCursor:
  86. case BottomLeftCornerResizeCursor:
  87. return MouseCursorHelpers::fromWebKitFile ("northEastSouthWestResizeCursor.png", 0.5f, 0.5f);
  88. case UpDownLeftRightResizeCursor:
  89. return MouseCursorHelpers::fromWebKitFile ("moveCursor.png", 0.5f, 0.5f);
  90. default:
  91. jassertfalse;
  92. break;
  93. }
  94. [c retain];
  95. return c;
  96. }
  97. }
  98. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  99. {
  100. [((NSCursor*) cursorHandle) release];
  101. }
  102. void MouseCursor::showInAllWindows() const
  103. {
  104. showInWindow (nullptr);
  105. }
  106. void MouseCursor::showInWindow (ComponentPeer*) const
  107. {
  108. NSCursor* c = (NSCursor*) getHandle();
  109. if (c == nil)
  110. c = [NSCursor arrowCursor];
  111. [c set];
  112. }
  113. #else
  114. void* CustomMouseCursorInfo::create() const { return nullptr; }
  115. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type) { return nullptr; }
  116. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool isStandard) {}
  117. void MouseCursor::showInAllWindows() const {}
  118. void MouseCursor::showInWindow (ComponentPeer*) const {}
  119. #endif