wined3d: Get rid of the offscreenBuffer field in struct wined3d_device.
[wine.git] / dlls / winemac.drv / cocoa_clipboard.m
blob1d1cb88d4c62a0969041e3bae7170a36cf372086
1 /*
2  * MACDRV Cocoa clipboard code
3  *
4  * Copyright 2012, 2013 Ken Thomases for CodeWeavers Inc.
5  *
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.
10  *
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.
15  *
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
19  */
21 #include "macdrv_cocoa.h"
22 #import "cocoa_app.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
35  */
36 int macdrv_is_pasteboard_owner(void)
38     __block int ret;
40     OnMainThread(^{
41         NSPasteboard* pb = [NSPasteboard generalPasteboard];
42         ret = ([pb changeCount] == owned_change_count);
43     });
45     return ret;
49 /***********************************************************************
50  *              macdrv_copy_pasteboard_types
51  *
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().
55  */
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],
69                                     nil];
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];
76     });
78     OnMainThread(^{
79         @try
80         {
81             NSPasteboard* local_pb = pb;
82             NSArray* types;
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]])
93             {
94                 NSMutableArray* newTypes = [BitmapOutputTypes mutableCopy];
95                 [newTypes removeObjectsInArray:types];
96                 types = [types arrayByAddingObjectsFromArray:newTypes];
97                 [newTypes release];
98             }
100             ret = (CFArrayRef)[types copy];
101         }
102         @catch (id e)
103         {
104             ERR(@"Exception discarded while copying pasteboard types: %@\n", e);
105         }
106     });
108     [pool release];
109     return ret;
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().
119  */
120 CFDataRef macdrv_copy_pasteboard_data(CFTypeRef pasteboard, CFStringRef type)
122     NSPasteboard* pb = (NSPasteboard*)pasteboard;
123     __block NSData* ret = nil;
125     OnMainThread(^{
126         @try
127         {
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];
132             else
133             {
134                 NSNumber* bitmapType = [BitmapOutputTypeMap objectForKey:(NSString*)type];
135                 if (bitmapType)
136                 {
137                     NSArray* reps = [NSBitmapImageRep imageRepsWithPasteboard:local_pb];
138                     ret = [NSBitmapImageRep representationOfImageRepsInArray:reps
139                                                                    usingType:[bitmapType unsignedIntegerValue]
140                                                                   properties:nil];
141                     ret = [ret copy];
142                 }
143             }
144         }
145         @catch (id e)
146         {
147             ERR(@"Exception discarded while copying pasteboard types: %@\n", e);
148         }
149     });
151     return (CFDataRef)ret;
155 /***********************************************************************
156  *              macdrv_clear_pasteboard
158  * Takes ownership of the Mac pasteboard and clears it of all data types.
159  */
160 void macdrv_clear_pasteboard(void)
162     OnMainThreadAsync(^{
163         @try
164         {
165             NSPasteboard* pb = [NSPasteboard generalPasteboard];
166             owned_change_count = [pb declareTypes:[NSArray array] owner:nil];
167         }
168         @catch (id e)
169         {
170             ERR(@"Exception discarded while clearing pasteboard: %@\n", e);
171         }
172     });
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
181  * type.
183  * Returns 0 on error, non-zero on success.
184  */
185 int macdrv_set_pasteboard_data(CFStringRef type, CFDataRef data, macdrv_window w)
187     __block int ret = 0;
188     WineWindow* window = (WineWindow*)w;
190     OnMainThread(^{
191         @try
192         {
193             NSPasteboard* pb = [NSPasteboard generalPasteboard];
194             NSInteger change_count = [pb addTypes:[NSArray arrayWithObject:(NSString*)type]
195                                             owner:window];
196             if (change_count)
197             {
198                 owned_change_count = change_count;
199                 if (data)
200                     ret = [pb setData:(NSData*)data forType:(NSString*)type];
201                 else
202                     ret = 1;
203             }
204         }
205         @catch (id e)
206         {
207             ERR(@"Exception discarded while copying pasteboard types: %@\n", e);
208         }
209     });
211     return ret;