Skip to content

Commit

Permalink
Improve clipboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Nov 3, 2024
1 parent 0cdd304 commit 916b26a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 44 deletions.
1 change: 1 addition & 0 deletions modules/gin_gui/gin_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace gin
#include "utilities/gin_openstreetmaps.cpp"
#include "utilities/gin_elevatedfilecopy.cpp"
#include "utilities/gin_layout.cpp"
#include "utilities/gin_platform.cpp"
#include "utilities/gin_systemclipboard.cpp"

#include "images/gin_imageutilities.cpp"
Expand Down
50 changes: 50 additions & 0 deletions modules/gin_gui/utilities/gin_platform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*==============================================================================
Copyright 2024 by Roland Rabien
For more information visit www.rabiensoftware.com
==============================================================================*/

//==============================================================================
#if JUCE_MAC
juce::Image nsImageToImage (NSImage* nsImage)
{
if (nsImage != nil)
{
if (CGImageRef cgImage = [nsImage CGImageForProposedRect:nullptr context:nil hints:nil])
{
const auto width = CGImageGetWidth (cgImage);
const auto height = CGImageGetHeight (cgImage);
const auto cgRect = CGRectMake (0, 0, (CGFloat) width, (CGFloat) height);
const juce::Image image (juce::Image::ARGB, (int) width, (int) height, true);

CGContextDrawImage (juce_getImageContext (image), cgRect, cgImage);

return image;
}
}
return {};
}

NSImage* imageToNSImage (const juce::Image& image)
{
const auto scaleFactor = 1.0f;

using ColorSpacePtr = juce::CFUniquePtr<CGColorSpaceRef>;
using ImagePtr = juce::CFUniquePtr<CGImageRef>;

NSImage* im = [[NSImage alloc] init];
auto requiredSize = NSMakeSize (image.getWidth() / scaleFactor, image.getHeight() / scaleFactor);

[im setSize: requiredSize];
ColorSpacePtr colourSpace { CGColorSpaceCreateWithName (kCGColorSpaceSRGB) };
ImagePtr imageRef { juce::juce_createCoreGraphicsImage (image, colourSpace.get()) };

NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef.get()];
[imageRep setSize: requiredSize];
[im addRepresentation: imageRep];
[imageRep release];
return im;
}
#endif

19 changes: 19 additions & 0 deletions modules/gin_gui/utilities/gin_platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*==============================================================================
Copyright 2024 by Roland Rabien
For more information visit www.rabiensoftware.com
==============================================================================*/

#pragma once

//==============================================================================

#if JUCE_MAC
namespace macOS
{
juce::Image nsImageToImage (NSImage* nsImage);

NSImage* imageToNSImage (const juce::Image& image);
}
#endif
46 changes: 2 additions & 44 deletions modules/gin_gui/utilities/gin_systemclipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,13 @@
==============================================================================*/

//==============================================================================
#if JUCE_MAC
juce::Image createJuceImage (NSImage* nsImage)
{
if (nsImage != nil)
{
[nsImage lockFocus];
NSBitmapImageRep* bitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect (0, 0, nsImage.size.width, nsImage.size.height)];
[nsImage unlockFocus];

NSDictionary* dic = [NSDictionary dictionary];
NSData* data = [bitmapRep representationUsingType:NSPNGFileType properties:dic];

auto res = juce::PNGImageFormat::loadFrom (data.bytes, data.length);
[bitmapRep release];

return res;
}
return {};
}

NSImage* createNSImage (const juce::Image& image)
{
const auto scaleFactor = 1.0f;

using ColorSpacePtr = juce::CFUniquePtr<CGColorSpaceRef>;
using ImagePtr = juce::CFUniquePtr<CGImageRef>;

NSImage* im = [[NSImage alloc] init];
auto requiredSize = NSMakeSize (image.getWidth() / scaleFactor, image.getHeight() / scaleFactor);

[im setSize: requiredSize];
ColorSpacePtr colourSpace { CGColorSpaceCreateWithName (kCGColorSpaceSRGB) };
ImagePtr imageRef { juce::juce_createCoreGraphicsImage (image, colourSpace.get()) };

NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef.get()];
[imageRep setSize: requiredSize];
[im addRepresentation: imageRep];
[imageRep release];
return im;
}
#endif


void SystemClipboard::copyImageToClipboard (const juce::Image& image)
{
#if JUCE_MAC
JUCE_AUTORELEASEPOOL
{
auto nsImage = createNSImage (image);
auto nsImage = imageToNSImage (image);

NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
Expand All @@ -80,7 +38,7 @@ juce::Image SystemClipboard::getImageFromClipboard()
NSArray* objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
NSImage* image = [objectsToPaste objectAtIndex:0];

return createJuceImage (image);
return nsImageToImage (image);
}
}
return {};
Expand Down

0 comments on commit 916b26a

Please sign in to comment.