yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
image_clipboard.mm
Go to the documentation of this file.
2
3#include <TargetConditionals.h>
4
5#import <Foundation/Foundation.h>
6#if TARGET_OS_OSX
7#import <AppKit/AppKit.h>
8#endif
9
10namespace yaze::util {
11
12#if TARGET_OS_OSX
13
14namespace {
15
16NSPasteboard* Board(const std::string& name) {
17 if (name.empty()) {
18 return [NSPasteboard generalPasteboard];
19 }
20 return [NSPasteboard
21 pasteboardWithName:[NSString stringWithUTF8String:name.c_str()]];
22}
23
24} // namespace
25
26bool ImageClipboardSupported() { return true; }
27
28absl::Status SetClipboardPng(const std::vector<uint8_t>& png,
29 const std::string& pasteboard) {
30 @autoreleasepool {
31 NSPasteboard* board = Board(pasteboard);
32 NSData* data = [NSData dataWithBytes:png.data() length:png.size()];
33 [board clearContents];
34 if (![board setData:data forType:NSPasteboardTypePNG]) {
35 return absl::InternalError("The clipboard refused the PNG image");
36 }
37 }
38 return absl::OkStatus();
39}
40
41absl::StatusOr<std::vector<uint8_t>> GetClipboardPng(
42 const std::string& pasteboard) {
43 @autoreleasepool {
44 NSPasteboard* board = Board(pasteboard);
45 NSData* data = [board dataForType:NSPasteboardTypePNG];
46 if (data == nil) {
47 NSData* tiff = [board dataForType:NSPasteboardTypeTIFF];
48 if (tiff != nil) {
49 NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:tiff];
50 data = [rep representationUsingType:NSBitmapImageFileTypePNG
51 properties:@{}];
52 }
53 }
54 if (data == nil || data.length == 0) {
55 return absl::NotFoundError("The clipboard holds no image");
56 }
57 const auto* bytes = static_cast<const uint8_t*>(data.bytes);
58 return std::vector<uint8_t>(bytes, bytes + data.length);
59 }
60}
61
62#else // iOS: UIPasteboard is not wired up yet.
63
64bool ImageClipboardSupported() { return false; }
65
66absl::Status SetClipboardPng(const std::vector<uint8_t>&, const std::string&) {
67 return absl::UnimplementedError("Image clipboard is not available here");
68}
69
70absl::StatusOr<std::vector<uint8_t>> GetClipboardPng(const std::string&) {
71 return absl::UnimplementedError("Image clipboard is not available here");
72}
73
74#endif
75
76} // namespace yaze::util
PNG images on the system clipboard. SDL's clipboard is text-only.
absl::StatusOr< std::vector< uint8_t > > GetClipboardPng(const std::string &)
bool ImageClipboardSupported()
absl::Status SetClipboardPng(const std::vector< uint8_t > &, const std::string &)
Replaces the clipboard contents with one PNG image.