1 /* gtkquartz.c: Utility functions used by the Quartz port
3 * Copyright (C) 2006 Imendio AB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gtkquartz.h"
27 _gtk_quartz_create_image_from_pixbuf (GdkPixbuf *pixbuf)
29 CGColorSpaceRef colorspace;
30 CGDataProviderRef data_provider;
34 int rowstride, pixbuf_width, pixbuf_height;
38 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
39 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
40 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
41 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
43 data = gdk_pixbuf_get_pixels (pixbuf);
45 colorspace = CGColorSpaceCreateDeviceRGB ();
46 data_provider = CGDataProviderCreateWithData (NULL, data, pixbuf_height * rowstride, NULL);
48 image = CGImageCreate (pixbuf_width, pixbuf_height, 8,
49 has_alpha ? 32 : 24, rowstride,
51 has_alpha ? kCGImageAlphaLast : 0,
52 data_provider, NULL, FALSE,
53 kCGRenderingIntentDefault);
55 CGDataProviderRelease (data_provider);
56 CGColorSpaceRelease (colorspace);
58 nsimage = [[NSImage alloc] initWithSize:NSMakeSize (pixbuf_width, pixbuf_height)];
61 context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
62 CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
64 [nsimage unlockFocus];
66 CGImageRelease (image);
72 target_to_pasteboard_type (const char *target)
74 if (strcmp (target, "UTF8_STRING") == 0)
75 return NSStringPboardType;
76 else if (strcmp (target, "image/tiff") == 0)
77 return NSTIFFPboardType;
78 else if (strcmp (target, "application/x-color") == 0)
79 return NSColorPboardType;
80 else if (strcmp (target, "text/uri-list") == 0)
81 return NSURLPboardType;
83 return [NSString stringWithUTF8String:target];
87 _gtk_quartz_target_list_to_pasteboard_types (GtkTargetList *target_list)
89 NSMutableSet *set = [[NSMutableSet alloc] init];
92 for (list = target_list->list; list; list = list->next)
94 GtkTargetPair *pair = list->data;
95 gchar *target = gdk_atom_name (pair->target);
96 [set addObject:target_to_pasteboard_type (target)];
100 return [set allObjects];
104 _gtk_quartz_target_entries_to_pasteboard_types (const GtkTargetEntry *targets,
107 NSMutableSet *set = [[NSMutableSet alloc] init];
110 for (i = 0; i < n_targets; i++)
112 [set addObject:target_to_pasteboard_type (targets[i].target)];
115 return [set allObjects];
119 _gtk_quartz_pasteboard_type_to_atom (NSString *type)
121 if ([type isEqualToString:NSStringPboardType])
122 return gdk_atom_intern_static_string ("UTF8_STRING");
123 else if ([type isEqualToString:NSTIFFPboardType])
124 return gdk_atom_intern_static_string ("image/tiff");
125 else if ([type isEqualToString:NSColorPboardType])
126 return gdk_atom_intern_static_string ("application/x-color");
127 else if ([type isEqualToString:NSURLPboardType])
128 return gdk_atom_intern_static_string ("text/uri-list");
130 return gdk_atom_intern ([type UTF8String], FALSE);
134 _gtk_quartz_pasteboard_types_to_atom_list (NSArray *array)
136 GList *result = NULL;
140 count = [array count];
142 for (i = 0; i < count; i++)
144 GdkAtom atom = _gtk_quartz_pasteboard_type_to_atom ([array objectAtIndex:i]);
146 result = g_list_prepend (result, GDK_ATOM_TO_POINTER (atom));
153 _gtk_quartz_get_selection_data_from_pasteboard (NSPasteboard *pasteboard,
157 GtkSelectionData *selection_data = NULL;
159 selection_data = g_slice_new0 (GtkSelectionData);
160 selection_data->selection = selection;
161 selection_data->target = target;
163 if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
165 NSString *s = [pasteboard stringForType:NSStringPboardType];
169 int len = [s length];
171 selection_data->type = target;
172 selection_data->format = 8;
173 selection_data->length = len;
174 selection_data->data = g_memdup ([s UTF8String], len + 1);
177 else if (target == gdk_atom_intern_static_string ("application/x-color"))
179 NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
180 colorUsingColorSpaceName:NSDeviceRGBColorSpace];
184 selection_data->target = target;
186 color[0] = 0xffff * [nscolor redComponent];
187 color[1] = 0xffff * [nscolor greenComponent];
188 color[2] = 0xffff * [nscolor blueComponent];
189 color[3] = 0xffff * [nscolor alphaComponent];
191 gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
193 else if (target == gdk_atom_intern_static_string ("text/uri-list"))
195 if ([[pasteboard types] containsObject:NSFilenamesPboardType])
198 NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
199 int n_files = [files count];
202 selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
204 uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
205 for (i = 0; i < n_files; ++i)
207 NSString* uriString = [files objectAtIndex:i];
208 uriString = [@"file://" stringByAppendingString:uriString];
209 uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
210 uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
214 gtk_selection_data_set_uris (selection_data, uris);
217 else if ([[pasteboard types] containsObject:NSURLPboardType])
220 NSURL *url = [NSURL URLFromPasteboard:pasteboard];
222 selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
224 uris[0] = (gchar *) [[url description] UTF8String];
227 gtk_selection_data_set_uris (selection_data, uris);
235 name = gdk_atom_name (target);
237 if (strcmp (name, "image/tiff") == 0)
238 data = [pasteboard dataForType:NSTIFFPboardType];
240 data = [pasteboard dataForType:[NSString stringWithUTF8String:name]];
246 selection_data->type = target;
247 selection_data->format = 8;
248 selection_data->length = [data length];
249 selection_data->data = g_malloc (selection_data->length + 1);
250 selection_data->data[selection_data->length] = '\0';
251 memcpy(selection_data->data, [data bytes], selection_data->length);
255 return selection_data;
259 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
260 GtkSelectionData *selection_data)
263 gchar *target = gdk_atom_name (selection_data->target);
265 type = target_to_pasteboard_type (target);
268 if ([type isEqualTo:NSStringPboardType])
269 [pasteboard setString:[NSString stringWithUTF8String:(const char *)selection_data->data]
271 else if ([type isEqualTo:NSColorPboardType])
273 guint16 *color = (guint16 *)selection_data->data;
274 float red, green, blue, alpha;
277 red = (float)color[0] / 0xffff;
278 green = (float)color[1] / 0xffff;
279 blue = (float)color[2] / 0xffff;
280 alpha = (float)color[3] / 0xffff;
282 nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
283 [nscolor writeToPasteboard:pasteboard];
285 else if ([type isEqualTo:NSURLPboardType])
290 count = gdk_text_property_to_utf8_list_for_display (selection_data->display,
291 gdk_atom_intern_static_string ("UTF8_STRING"),
292 selection_data->format,
293 selection_data->data,
294 selection_data->length,
302 result = g_uri_list_extract_uris (list[0]);
304 url = [NSURL URLWithString:[NSString stringWithUTF8String:result[0]]];
305 [url writeToPasteboard:pasteboard];
313 [pasteboard setData:[NSData dataWithBytesNoCopy:selection_data->data
314 length:selection_data->length