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"
24 #include "gtkselectionprivate.h"
25 #include <gdk/quartz/gdkquartz.h>
28 _gtk_quartz_create_image_from_pixbuf (GdkPixbuf *pixbuf)
30 CGColorSpaceRef colorspace;
31 CGDataProviderRef data_provider;
35 int rowstride, pixbuf_width, pixbuf_height;
39 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
40 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
41 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
42 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
44 data = gdk_pixbuf_get_pixels (pixbuf);
46 colorspace = CGColorSpaceCreateDeviceRGB ();
47 data_provider = CGDataProviderCreateWithData (NULL, data, pixbuf_height * rowstride, NULL);
49 image = CGImageCreate (pixbuf_width, pixbuf_height, 8,
50 has_alpha ? 32 : 24, rowstride,
52 has_alpha ? kCGImageAlphaLast : 0,
53 data_provider, NULL, FALSE,
54 kCGRenderingIntentDefault);
56 CGDataProviderRelease (data_provider);
57 CGColorSpaceRelease (colorspace);
59 nsimage = [[NSImage alloc] initWithSize:NSMakeSize (pixbuf_width, pixbuf_height)];
62 context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
63 CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
65 [nsimage unlockFocus];
67 CGImageRelease (image);
73 target_to_pasteboard_type (const char *target)
75 if (strcmp (target, "UTF8_STRING") == 0)
76 return NSStringPboardType;
77 else if (strcmp (target, "image/tiff") == 0)
78 return NSTIFFPboardType;
79 else if (strcmp (target, "application/x-color") == 0)
80 return NSColorPboardType;
81 else if (strcmp (target, "text/uri-list") == 0)
82 return NSURLPboardType;
84 return [NSString stringWithUTF8String:target];
88 _gtk_quartz_target_list_to_pasteboard_types (GtkTargetList *target_list)
90 NSMutableSet *set = [[NSMutableSet alloc] init];
93 for (list = target_list->list; list; list = list->next)
95 GtkTargetPair *pair = list->data;
96 gchar *target = gdk_atom_name (pair->target);
97 [set addObject:target_to_pasteboard_type (target)];
105 _gtk_quartz_target_entries_to_pasteboard_types (const GtkTargetEntry *targets,
108 NSMutableSet *set = [[NSMutableSet alloc] init];
111 for (i = 0; i < n_targets; i++)
113 [set addObject:target_to_pasteboard_type (targets[i].target)];
120 _gtk_quartz_pasteboard_type_to_atom (NSString *type)
122 if ([type isEqualToString:NSStringPboardType])
123 return gdk_atom_intern_static_string ("UTF8_STRING");
124 else if ([type isEqualToString:NSTIFFPboardType])
125 return gdk_atom_intern_static_string ("image/tiff");
126 else if ([type isEqualToString:NSColorPboardType])
127 return gdk_atom_intern_static_string ("application/x-color");
128 else if ([type isEqualToString:NSURLPboardType])
129 return gdk_atom_intern_static_string ("text/uri-list");
131 return gdk_atom_intern ([type UTF8String], FALSE);
135 _gtk_quartz_pasteboard_types_to_atom_list (NSArray *array)
137 GList *result = NULL;
141 count = [array count];
143 for (i = 0; i < count; i++)
145 GdkAtom atom = _gtk_quartz_pasteboard_type_to_atom ([array objectAtIndex:i]);
147 result = g_list_prepend (result, GDK_ATOM_TO_POINTER (atom));
154 _gtk_quartz_get_selection_data_from_pasteboard (NSPasteboard *pasteboard,
158 GtkSelectionData *selection_data = NULL;
160 selection_data = g_slice_new0 (GtkSelectionData);
161 selection_data->selection = selection;
162 selection_data->target = target;
164 if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
166 NSString *s = [pasteboard stringForType:NSStringPboardType];
170 const char *utf8_string = [s UTF8String];
172 gtk_selection_data_set (selection_data,
174 (guchar *)utf8_string, strlen (utf8_string));
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 gtk_selection_data_set (selection_data,
248 [data bytes], [data length]);
252 return selection_data;
256 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
257 GtkSelectionData *selection_data)
266 target = gdk_atom_name (gtk_selection_data_get_target (selection_data));
267 display = gtk_selection_data_get_display (selection_data);
268 format = gtk_selection_data_get_format (selection_data);
269 data = gtk_selection_data_get_data (selection_data);
270 length = gtk_selection_data_get_length (selection_data);
272 type = target_to_pasteboard_type (target);
275 if ([type isEqualTo:NSStringPboardType])
276 [pasteboard setString:[NSString stringWithUTF8String:(const char *)data]
278 else if ([type isEqualTo:NSColorPboardType])
280 guint16 *color = (guint16 *)data;
281 float red, green, blue, alpha;
284 red = (float)color[0] / 0xffff;
285 green = (float)color[1] / 0xffff;
286 blue = (float)color[2] / 0xffff;
287 alpha = (float)color[3] / 0xffff;
289 nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
290 [nscolor writeToPasteboard:pasteboard];
292 else if ([type isEqualTo:NSURLPboardType])
296 uris = gtk_selection_data_get_uris (selection_data);
301 url = [NSURL URLWithString:[NSString stringWithUTF8String:uris[0]]];
302 [url writeToPasteboard:pasteboard];
307 [pasteboard setData:[NSData dataWithBytesNoCopy:(void *)data