1
0
forked from gmp/clip
clip/ns/pasteboard.go

346 lines
7.8 KiB
Go
Raw Permalink Normal View History

2019-10-01 09:33:35 -04:00
package ns
/*
#cgo CFLAGS: -x objective-c -fno-objc-arc
#cgo LDFLAGS: -framework AppKit -framework Foundation
#pragma clang diagnostic ignored "-Wformat-security"
#import <Foundation/Foundation.h>
#import <AppKit/NSPasteboard.h>
void
NSObject_inst_Release(void* o) {
@autoreleasepool {
[(NSObject*)o release];
}
}
void
NSString_inst_Release(void* o) {
@autoreleasepool {
[(NSString*)o release];
}
}
const void* _Nullable
NSString_inst_UTF8String(void* o) {
const char* _Nullable ret;
@autoreleasepool {
ret = strdup([(NSString*)o UTF8String]);
}
return ret;
}
void
NSPasteboard_inst_Release(void* o) {
@autoreleasepool {
[(NSPasteboard*)o release];
}
}
void* _Nullable
NSString_StringWithUTF8String(void* nullTerminatedCString) {
NSString* _Nullable ret;
@autoreleasepool {
ret = [NSString stringWithUTF8String:nullTerminatedCString];
if(ret != nil) { [ret retain]; }
}
return ret;
}
void* _Nonnull
NSPasteboard_GeneralPasteboard() {
NSPasteboard* _Nonnull ret;
@autoreleasepool {
ret = [NSPasteboard generalPasteboard];
}
return ret;
}
void
NSPasteboard_inst_ClearContents(void* o) {
@autoreleasepool {
[(NSPasteboard*)o clearContents];
}
}
BOOL
NSPasteboard_inst_SetString(void* o, void* string) {
BOOL ret;
@autoreleasepool {
ret = [(NSPasteboard*)o setString:string forType:NSPasteboardTypeString];
}
return ret;
}
void* _Nullable
NSPasteboard_inst_GetString(void* o) {
NSString* _Nullable ret;
@autoreleasepool {
ret = [(NSPasteboard*)o stringForType:NSPasteboardTypeString];
if (ret != nil && ret != o) { [ret retain]; }
}
return ret;
}
void
NSPasteboard_inst_PasteFileList(void* o, void* string) {
@autoreleasepool {
NSArray *values = [(NSString*)string componentsSeparatedByString:@"\n"];
NSMutableArray *urls = [NSMutableArray arrayWithCapacity:values.count + 1];
for (NSString *value in values) {
id transformedValue = [NSURL fileURLWithPath:value isDirectory:NO];
[urls addObject:transformedValue];
}
NSString* pathPerLine = [values componentsJoinedByString:@"\n"];
NSPasteboard* pasteboard = (NSPasteboard*)o;
[pasteboard clearContents];
[pasteboard writeObjects:urls];
//Now add the pathsPerLine as a string
[pasteboard setString:pathPerLine forType:NSPasteboardTypeString];
}
}
void* _Nullable
NSPasteboard_inst_GetFileList(void* o) {
NSString* _Nullable ret;
@autoreleasepool {
NSArray *classes = [NSArray arrayWithObject:[NSURL class]];
NSDictionary *options = [NSDictionary dictionaryWithObject:
[NSNumber numberWithBool:YES] forKey:NSPasteboardURLReadingFileURLsOnlyKey];
NSArray *fileURLs =
[(NSPasteboard*)o readObjectsForClasses:classes options:options];
NSMutableArray *paths = [NSMutableArray arrayWithCapacity:fileURLs.count];
for (NSURL *value in fileURLs) {
id transformedValue = value.absoluteString;
[paths addObject:transformedValue];
}
ret = [paths componentsJoinedByString:@"\n"];
if (ret != nil && ret != o) { [ret retain]; }
}
return ret;
}
BOOL
NSPasteboard_inst_GetImage(void* o, void* filePath) {
BOOL ret;
@autoreleasepool {
NSData *data = [(NSPasteboard*)o dataForType:NSPasteboardTypeTIFF];
if (data != nil && data != o) {
// NSImage *img = [[NSImage alloc] initWithData:data];
//NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"image.tiff"];
[data writeToFile: (NSString*)filePath atomically: NO];
ret = true;
} else
{
ret = false;
}
}
return ret;
}
2019-10-01 09:33:35 -04:00
*/
import "C"
import (
"runtime"
"strings"
"unsafe"
2019-10-01 09:33:35 -04:00
)
type Id struct {
ptr unsafe.Pointer
}
const FilesDelimeter = "\n"
func (o *Id) Ptr() unsafe.Pointer {
if o == nil {
return nil
}
return o.ptr
2019-10-01 09:33:35 -04:00
}
type NSObject interface {
Ptr() unsafe.Pointer
2019-10-01 09:33:35 -04:00
}
func (o *Id) Release() {
C.NSObject_inst_Release(o.Ptr())
runtime.KeepAlive(o)
2019-10-01 09:33:35 -04:00
}
func (o *NSPasteboard) Release() {
C.NSPasteboard_inst_Release(o.Ptr())
runtime.KeepAlive(o)
2019-10-01 09:33:35 -04:00
}
func (o *NSString) Release() {
C.NSString_inst_Release(o.Ptr())
runtime.KeepAlive(o)
2019-10-01 09:33:35 -04:00
}
func (c *Char) Free() {
C.free(unsafe.Pointer(c))
2019-10-01 09:33:35 -04:00
}
type BOOL C.uchar
type NSString struct{ Id }
func (o *NSString) Ptr() unsafe.Pointer {
if o == nil {
return nil
}
return o.ptr
}
2019-10-01 09:33:35 -04:00
func (o *Id) NSString() *NSString {
return (*NSString)(unsafe.Pointer(o))
2019-10-01 09:33:35 -04:00
}
func (o *NSString) UTF8String() *Char {
ret := (*Char)(unsafe.Pointer(C.NSString_inst_UTF8String(o.Ptr())))
runtime.KeepAlive(o)
return ret
2019-10-01 09:33:35 -04:00
}
func (o *NSString) String() string {
utf8 := o.UTF8String()
ret := utf8.String()
utf8.Free()
runtime.KeepAlive(o)
return ret
2019-10-01 09:33:35 -04:00
}
type NSPasteboard struct{ Id }
func (o *NSPasteboard) Ptr() unsafe.Pointer {
if o == nil {
return nil
}
return o.ptr
}
2019-10-01 09:33:35 -04:00
func (o *Id) NSPasteboard() *NSPasteboard {
return (*NSPasteboard)(unsafe.Pointer(o))
2019-10-01 09:33:35 -04:00
}
type Char C.char
func CharWithGoString(s string) *Char {
return (*Char)(unsafe.Pointer(C.CString(s)))
2019-10-01 09:33:35 -04:00
}
func (c *Char) String() string {
return C.GoString((*C.char)(c))
2019-10-01 09:33:35 -04:00
}
func NSStringWithUTF8String(nullTerminatedCString *Char) *NSString {
ret := &NSString{}
ret.ptr = unsafe.Pointer(C.NSString_StringWithUTF8String(unsafe.Pointer(nullTerminatedCString)))
if ret.ptr == nil {
return ret
}
runtime.SetFinalizer(ret, func(o *NSString) {
o.Release()
})
return ret
2019-10-01 09:33:35 -04:00
}
func NSStringWithGoString(string string) *NSString {
string_chr := CharWithGoString(string)
defer string_chr.Free()
ret := NSStringWithUTF8String(string_chr)
return ret
2019-10-01 09:33:35 -04:00
}
func NSPasteboardGeneralPasteboard() *NSPasteboard {
ret := &NSPasteboard{}
ret.ptr = unsafe.Pointer(C.NSPasteboard_GeneralPasteboard())
if ret.ptr == nil {
return ret
}
return ret
2019-10-01 09:33:35 -04:00
}
func (o *NSPasteboard) ClearContents() {
C.NSPasteboard_inst_ClearContents(o.Ptr())
runtime.KeepAlive(o)
2019-10-01 09:33:35 -04:00
}
func (o *NSPasteboard) SetString(s string) bool {
string := NSStringWithGoString(s)
ret := (C.NSPasteboard_inst_SetString(o.Ptr(), string.Ptr())) != 0
runtime.KeepAlive(o)
2019-10-01 09:33:35 -04:00
runtime.KeepAlive(string)
return ret
2019-10-01 09:33:35 -04:00
}
func (o *NSPasteboard) GetString() *NSString {
ret := &NSString{}
ret.ptr = unsafe.Pointer(C.NSPasteboard_inst_GetString(o.Ptr()))
if ret.ptr == nil {
runtime.KeepAlive(o)
return ret
}
if ret.ptr == o.ptr {
runtime.KeepAlive(o)
return (*NSString)(unsafe.Pointer(o))
}
runtime.SetFinalizer(ret, func(o *NSString) {
o.Release()
})
runtime.KeepAlive(o)
return ret
2019-10-01 09:33:35 -04:00
}
func (o *NSPasteboard) PasteFileList(values []string) {
inString := strings.Join(values, FilesDelimeter)
str := NSStringWithGoString(inString)
C.NSPasteboard_inst_PasteFileList(o.Ptr(), str.Ptr())
runtime.KeepAlive(o)
runtime.KeepAlive(str)
return
}
func (o *NSPasteboard) GetFileList() *NSString {
ret := &NSString{}
ret.ptr = unsafe.Pointer(C.NSPasteboard_inst_GetFileList(o.Ptr()))
if ret.ptr == nil {
runtime.KeepAlive(o)
return ret
}
if ret.ptr == o.ptr {
runtime.KeepAlive(o)
return (*NSString)(unsafe.Pointer(o))
}
runtime.SetFinalizer(ret, func(o *NSString) {
o.Release()
})
runtime.KeepAlive(o)
return ret
}
func (o *NSPasteboard) GetImage(filePath string) bool {
string := NSStringWithGoString(filePath)
ret := (C.NSPasteboard_inst_GetImage(o.Ptr(), string.Ptr())) != 0
runtime.KeepAlive(o)
runtime.KeepAlive(string)
return ret
}