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, see <http://www.gnu.org/licenses/>.
21 #include "gtkquartz.h"
22 #include "gtkselectionprivate.h"
23 #include <gdk/quartz/gdkquartz.h>
26 _gtk_quartz_create_image_from_pixbuf (GdkPixbuf *pixbuf)
28 CGColorSpaceRef colorspace;
29 CGDataProviderRef data_provider;
33 int rowstride, pixbuf_width, pixbuf_height;
38 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
39 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
40 g_return_val_if_fail (pixbuf_width != 0 && pixbuf_height != 0, NULL);
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)];
60 nsimage_size = [nsimage size];
61 if (nsimage_size.width == 0.0 && nsimage_size.height == 0.0)
64 g_critical ("%s returned a zero-sized image", G_STRFUNC);
69 context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
70 CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
72 [nsimage unlockFocus];
74 CGImageRelease (image);
80 target_to_pasteboard_type (const char *target)
82 if (strcmp (target, "UTF8_STRING") == 0)
83 return NSStringPboardType;
84 else if (strcmp (target, "image/tiff") == 0)
85 return NSTIFFPboardType;
86 else if (strcmp (target, "application/x-color") == 0)
87 return NSColorPboardType;
88 else if (strcmp (target, "text/uri-list") == 0)
89 return NSURLPboardType;
91 return [NSString stringWithUTF8String:target];
95 _gtk_quartz_target_list_to_pasteboard_types (GtkTargetList *target_list)
97 NSMutableSet *set = [[NSMutableSet alloc] init];
100 for (list = target_list->list; list; list = list->next)
102 GtkTargetPair *pair = list->data;
103 gchar *target = gdk_atom_name (pair->target);
104 g_return_val_if_fail (pair->flags < 16, NULL);
105 [set addObject:target_to_pasteboard_type (target)];
113 _gtk_quartz_target_entries_to_pasteboard_types (const GtkTargetEntry *targets,
116 NSMutableSet *set = [[NSMutableSet alloc] init];
119 for (i = 0; i < n_targets; i++)
121 [set addObject:target_to_pasteboard_type (targets[i].target)];
128 _gtk_quartz_pasteboard_type_to_atom (NSString *type)
130 if ([type isEqualToString:NSStringPboardType])
131 return gdk_atom_intern_static_string ("UTF8_STRING");
132 else if ([type isEqualToString:NSTIFFPboardType])
133 return gdk_atom_intern_static_string ("image/tiff");
134 else if ([type isEqualToString:NSColorPboardType])
135 return gdk_atom_intern_static_string ("application/x-color");
136 else if ([type isEqualToString:NSURLPboardType])
137 return gdk_atom_intern_static_string ("text/uri-list");
139 return gdk_atom_intern ([type UTF8String], FALSE);
143 _gtk_quartz_pasteboard_types_to_atom_list (NSArray *array)
145 GList *result = NULL;
149 count = [array count];
151 for (i = 0; i < count; i++)
153 GdkAtom atom = _gtk_quartz_pasteboard_type_to_atom ([array objectAtIndex:i]);
155 result = g_list_prepend (result, GDK_ATOM_TO_POINTER (atom));
162 _gtk_quartz_get_selection_data_from_pasteboard (NSPasteboard *pasteboard,
166 GtkSelectionData *selection_data = NULL;
168 selection_data = g_slice_new0 (GtkSelectionData);
169 selection_data->selection = selection;
170 selection_data->target = target;
171 if (!selection_data->display)
172 selection_data->display = gdk_display_get_default ();
173 if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
175 NSString *s = [pasteboard stringForType:NSStringPboardType];
179 const char *utf8_string = [s UTF8String];
181 gtk_selection_data_set (selection_data,
183 (guchar *)utf8_string, strlen (utf8_string));
186 else if (target == gdk_atom_intern_static_string ("application/x-color"))
188 NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
189 colorUsingColorSpaceName:NSDeviceRGBColorSpace];
193 selection_data->target = target;
195 color[0] = 0xffff * [nscolor redComponent];
196 color[1] = 0xffff * [nscolor greenComponent];
197 color[2] = 0xffff * [nscolor blueComponent];
198 color[3] = 0xffff * [nscolor alphaComponent];
200 gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
202 else if (target == gdk_atom_intern_static_string ("text/uri-list"))
204 if ([[pasteboard types] containsObject:NSFilenamesPboardType])
207 NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
208 int n_files = [files count];
211 selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
213 uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
214 for (i = 0; i < n_files; ++i)
216 NSString* uriString = [files objectAtIndex:i];
217 uriString = [@"file://" stringByAppendingString:uriString];
218 uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
219 uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
223 gtk_selection_data_set_uris (selection_data, uris);
226 else if ([[pasteboard types] containsObject:NSURLPboardType])
229 NSURL *url = [NSURL URLFromPasteboard:pasteboard];
231 selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
233 uris[0] = (gchar *) [[url description] UTF8String];
236 gtk_selection_data_set_uris (selection_data, uris);
244 name = gdk_atom_name (target);
246 if (strcmp (name, "image/tiff") == 0)
247 data = [pasteboard dataForType:NSTIFFPboardType];
249 data = [pasteboard dataForType:[NSString stringWithUTF8String:name]];
255 gtk_selection_data_set (selection_data,
257 [data bytes], [data length]);
261 return selection_data;
265 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
266 GtkSelectionData *selection_data)
275 target = gdk_atom_name (gtk_selection_data_get_target (selection_data));
276 display = gtk_selection_data_get_display (selection_data);
277 format = gtk_selection_data_get_format (selection_data);
278 data = gtk_selection_data_get_data (selection_data);
279 length = gtk_selection_data_get_length (selection_data);
281 type = target_to_pasteboard_type (target);
284 if ([type isEqualTo:NSStringPboardType])
285 [pasteboard setString:[NSString stringWithUTF8String:(const char *)data]
287 else if ([type isEqualTo:NSColorPboardType])
289 guint16 *color = (guint16 *)data;
290 float red, green, blue, alpha;
293 red = (float)color[0] / 0xffff;
294 green = (float)color[1] / 0xffff;
295 blue = (float)color[2] / 0xffff;
296 alpha = (float)color[3] / 0xffff;
298 nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
299 [nscolor writeToPasteboard:pasteboard];
301 else if ([type isEqualTo:NSURLPboardType])
305 uris = gtk_selection_data_get_uris (selection_data);
310 url = [NSURL URLWithString:[NSString stringWithUTF8String:uris[0]]];
311 [url writeToPasteboard:pasteboard];
316 [pasteboard setData:[NSData dataWithBytesNoCopy:(void *)data
322 #ifdef QUARTZ_RELOCATION
324 /* Bundle-based functions for various directories. These almost work
325 * even when the application isn't in a bundle, becuase mainBundle
326 * paths point to the bin directory in that case. It's a simple matter
327 * to test for that and remove the last element.
331 get_bundle_path (void)
333 static gchar *path = NULL;
337 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
338 gchar *resource_path = g_strdup ([[[NSBundle mainBundle] resourcePath] UTF8String]);
342 base = g_path_get_basename (resource_path);
343 if (strcmp (base, "bin") == 0)
344 path = g_path_get_dirname (resource_path);
346 path = strdup (resource_path);
348 g_free (resource_path);
356 _gtk_get_datadir (void)
358 static gchar *path = NULL;
361 path = g_build_filename (get_bundle_path (), "share", NULL);
367 _gtk_get_libdir (void)
369 static gchar *path = NULL;
372 path = g_build_filename (get_bundle_path (), "lib", NULL);
378 _gtk_get_localedir (void)
380 static gchar *path = NULL;
383 path = g_build_filename (get_bundle_path (), "share", "locale", NULL);
389 _gtk_get_sysconfdir (void)
391 static gchar *path = NULL;
394 path = g_build_filename (get_bundle_path (), "etc", NULL);
400 _gtk_get_data_prefix (void)
402 return get_bundle_path ();
405 #endif /* QUARTZ_RELOCATION */