2 * MACDRV Cocoa clipboard code
4 * Copyright 2012, 2013 Ken Thomases for CodeWeavers Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "macdrv_cocoa.h"
23 #import "cocoa_event.h"
26 static int owned_change_count = -1;
28 static NSArray* BitmapOutputTypes;
29 static NSDictionary* BitmapOutputTypeMap;
30 static dispatch_once_t BitmapOutputTypesInitOnce;
33 /***********************************************************************
34 * macdrv_is_pasteboard_owner
36 int macdrv_is_pasteboard_owner(void)
41 NSPasteboard* pb = [NSPasteboard generalPasteboard];
42 ret = ([pb changeCount] == owned_change_count);
49 /***********************************************************************
50 * macdrv_copy_pasteboard_types
52 * Returns an array of UTI strings for the types of data available on
53 * the pasteboard, or NULL on error. The caller is responsible for
54 * releasing the returned array with CFRelease().
56 CFArrayRef macdrv_copy_pasteboard_types(CFTypeRef pasteboard)
58 NSPasteboard* pb = (NSPasteboard*)pasteboard;
59 __block CFArrayRef ret = NULL;
60 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
62 dispatch_once(&BitmapOutputTypesInitOnce, ^{
63 NSArray* bitmapFileTypes = [NSArray arrayWithObjects:
64 [NSNumber numberWithUnsignedInteger:NSTIFFFileType],
65 [NSNumber numberWithUnsignedInteger:NSPNGFileType],
66 [NSNumber numberWithUnsignedInteger:NSBMPFileType],
67 [NSNumber numberWithUnsignedInteger:NSGIFFileType],
68 [NSNumber numberWithUnsignedInteger:NSJPEGFileType],
71 BitmapOutputTypes = [[NSArray alloc] initWithObjects:@"public.tiff", @"public.png",
72 @"com.microsoft.bmp", @"com.compuserve.gif", @"public.jpeg", nil];
74 BitmapOutputTypeMap = [[NSDictionary alloc] initWithObjects:bitmapFileTypes
75 forKeys:BitmapOutputTypes];
81 NSPasteboard* local_pb = pb;
84 if (!local_pb) local_pb = [NSPasteboard generalPasteboard];
85 types = [local_pb types];
87 // If there are any types understood by NSBitmapImageRep, then we
88 // can offer all of the types that it can output, too. For example,
89 // if TIFF is on the pasteboard, we can offer PNG, BMP, etc. to the
90 // Windows program. We'll convert on demand.
91 if ([types firstObjectCommonWithArray:[NSBitmapImageRep imageTypes]] ||
92 [types firstObjectCommonWithArray:[NSBitmapImageRep imagePasteboardTypes]])
94 NSMutableArray* newTypes = [BitmapOutputTypes mutableCopy];
95 [newTypes removeObjectsInArray:types];
96 types = [types arrayByAddingObjectsFromArray:newTypes];
100 ret = (CFArrayRef)[types copy];
104 ERR(@"Exception discarded while copying pasteboard types: %@\n", e);
113 /***********************************************************************
114 * macdrv_copy_pasteboard_data
116 * Returns the pasteboard data for a specified type, or NULL on error or
117 * if there's no such type on the pasteboard. The caller is responsible
118 * for releasing the returned data object with CFRelease().
120 CFDataRef macdrv_copy_pasteboard_data(CFTypeRef pasteboard, CFStringRef type)
122 NSPasteboard* pb = (NSPasteboard*)pasteboard;
123 __block NSData* ret = nil;
128 NSPasteboard* local_pb = pb;
129 if (!local_pb) local_pb = [NSPasteboard generalPasteboard];
130 if ([local_pb availableTypeFromArray:[NSArray arrayWithObject:(NSString*)type]])
131 ret = [[local_pb dataForType:(NSString*)type] copy];
134 NSNumber* bitmapType = [BitmapOutputTypeMap objectForKey:(NSString*)type];
137 NSArray* reps = [NSBitmapImageRep imageRepsWithPasteboard:local_pb];
138 ret = [NSBitmapImageRep representationOfImageRepsInArray:reps
139 usingType:[bitmapType unsignedIntegerValue]
147 ERR(@"Exception discarded while copying pasteboard types: %@\n", e);
151 return (CFDataRef)ret;
155 /***********************************************************************
156 * macdrv_clear_pasteboard
158 * Takes ownership of the Mac pasteboard and clears it of all data types.
160 void macdrv_clear_pasteboard(void)
165 NSPasteboard* pb = [NSPasteboard generalPasteboard];
166 owned_change_count = [pb declareTypes:[NSArray array] owner:nil];
170 ERR(@"Exception discarded while clearing pasteboard: %@\n", e);
176 /***********************************************************************
177 * macdrv_set_pasteboard_data
179 * Sets the pasteboard data for a specified type. Replaces any data of
180 * that type already on the pasteboard. If data is NULL, promises the
183 * Returns 0 on error, non-zero on success.
185 int macdrv_set_pasteboard_data(CFStringRef type, CFDataRef data, macdrv_window w)
188 WineWindow* window = (WineWindow*)w;
193 NSPasteboard* pb = [NSPasteboard generalPasteboard];
194 NSInteger change_count = [pb addTypes:[NSArray arrayWithObject:(NSString*)type]
198 owned_change_count = change_count;
200 ret = [pb setData:(NSData*)data forType:(NSString*)type];
207 ERR(@"Exception discarded while copying pasteboard types: %@\n", e);